home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / bfd / ieee.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  71KB  |  2,950 lines

  1. /* bfd back-end for ieee-695 objects.
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Written by Steve Chamberlain of Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* IEEE 695 format is a stream of records, which we parse using a simple one-
  22.    token (which is one byte in this lexicon) lookahead recursive decent
  23.    parser.  */
  24.  
  25. #include "bfd.h"
  26. #include "sysdep.h"
  27. #include "libbfd.h"
  28. #include "ieee.h"
  29. #include "libieee.h"
  30.  
  31. /* Functions for writing to ieee files in the strange way that the
  32.    standard requires. */
  33.  
  34. static void
  35. DEFUN(ieee_write_byte,(abfd, byte),
  36.       bfd *abfd AND
  37.       bfd_byte byte)
  38. {
  39.   bfd_write((PTR)&byte, 1, 1, abfd);
  40. }
  41.  
  42.  
  43. static void
  44. DEFUN(ieee_write_twobyte,(abfd, twobyte),
  45.       bfd *abfd AND
  46.       int twobyte)
  47. {
  48.   bfd_byte b[2];
  49.   b[1] = twobyte & 0xff;
  50.   b[0] = twobyte >> 8;
  51.   bfd_write((PTR)&b[0], 1, 2, abfd);
  52. }
  53.  
  54.  
  55.  
  56. static void
  57. DEFUN(ieee_write_2bytes,(abfd, bytes),
  58.       bfd *abfd AND
  59.       int bytes)
  60. {
  61.   bfd_byte buffer[2];
  62.   buffer[0] = bytes >> 8;
  63.   buffer[1] = bytes & 0xff;
  64.  
  65.   bfd_write((PTR)buffer, 1, 2, abfd);
  66. }
  67.  
  68. static void
  69. DEFUN(ieee_write_int,(abfd, value),
  70.       bfd *abfd AND
  71.       bfd_vma value)
  72. {
  73.   if (((unsigned)value) <= 127) {
  74.     ieee_write_byte(abfd, value);
  75.   }
  76.   else {
  77.     unsigned int length;
  78.     /* How many significant bytes ? */
  79.     /* FIXME FOR LONGER INTS */
  80.     if (value & 0xff000000) {
  81.       length = 4;
  82.     }
  83.     else if (value & 0x00ff0000) {
  84.       length  = 3;
  85.     }
  86.     else if (value & 0x0000ff00) {
  87.       length = 2;
  88.     }
  89.     else length = 1;
  90.  
  91.     ieee_write_byte(abfd, (int)ieee_number_repeat_start_enum + length);
  92.     switch (length) {
  93.     case 4:
  94.       ieee_write_byte(abfd, value >> 24);
  95.     case 3:
  96.       ieee_write_byte(abfd, value >> 16);
  97.     case 2:
  98.       ieee_write_byte(abfd, value >> 8);
  99.     case 1:
  100.       ieee_write_byte(abfd, value);
  101.     }
  102.   }
  103. }
  104.  
  105. static void
  106. DEFUN(ieee_write_id,(abfd, id),
  107.       bfd *abfd AND
  108.       CONST char *id)
  109. {
  110.   size_t length = strlen(id);
  111.   if (length >= 0 && length <= 127) {
  112.     ieee_write_byte(abfd, length);
  113.   }
  114.   else if (length < 255) {
  115.     ieee_write_byte(abfd, ieee_extension_length_1_enum);
  116.     ieee_write_byte(abfd, length);
  117.   }
  118.   else if (length < 65535) {
  119.     ieee_write_byte(abfd, ieee_extension_length_2_enum);
  120.     ieee_write_byte(abfd, length >> 8);
  121.     ieee_write_byte(abfd, length & 0xff);  
  122.   }
  123.   else {
  124.     BFD_FAIL();
  125.   }
  126.   bfd_write((PTR)id, 1, length, abfd);
  127. }
  128. /***************************************************************************
  129. Functions for reading from ieee files in the strange way that the
  130. standard requires:
  131. */
  132.  
  133.  
  134. #define this_byte(ieee) *((ieee)->input_p)
  135. #define next_byte(ieee) ((ieee)->input_p++)
  136. #define this_byte_and_next(ieee) (*((ieee)->input_p++))
  137.  
  138.  
  139. static unsigned short 
  140. DEFUN(read_2bytes,(ieee),
  141.    common_header_type *ieee)
  142. {
  143.   unsigned  char c1 = this_byte_and_next(ieee);
  144.   unsigned  char c2 = this_byte_and_next(ieee);
  145.   return (c1<<8 ) | c2;
  146.  
  147. }
  148.  
  149. static void
  150. DEFUN(bfd_get_string,(ieee, string, length),
  151.     common_header_type *ieee AND
  152.       char *string AND
  153.       size_t length)
  154. {
  155.   size_t i;
  156.   for (i= 0; i < length; i++) {
  157.     string[i] = this_byte_and_next(ieee);
  158.   }
  159. }
  160.  
  161. static char *
  162. DEFUN(read_id,(ieee),
  163.   common_header_type *ieee)
  164. {
  165.   size_t length;
  166.   char *string;
  167.   length = this_byte_and_next(ieee);
  168.   if (length >= 0x00 && length <= 0x7f) {
  169.     /* Simple string of length 0 to 127 */
  170.   }
  171.   else if (length == 0xde) {
  172.     /* Length is next byte, allowing 0..255 */
  173.     length = this_byte_and_next(ieee);
  174.   }
  175.   else if (length == 0xdf) {
  176.     /* Length is next two bytes, allowing 0..65535 */
  177.     length = this_byte_and_next(ieee) ;
  178.     length = (length * 256) + this_byte_and_next(ieee);
  179.   }
  180.   /* Buy memory and read string */
  181.   string = bfd_alloc(ieee->abfd, length+1);
  182.   bfd_get_string(ieee, string, length);
  183.   string[length] = 0;
  184.   return string;
  185. }
  186.  
  187. static void
  188. DEFUN(ieee_write_expression,(abfd, value, section, symbol, pcrel, index),
  189.       bfd*abfd AND
  190.       bfd_vma value AND
  191.       asection *section AND
  192.       asymbol *symbol AND
  193.       boolean pcrel AND
  194.     unsigned int index)
  195. {
  196.   unsigned int plus_count = 0;
  197.   if (value != 0)
  198.     ieee_write_int(abfd, value);
  199.  
  200.   if (section != (asection *)NULL) {
  201.     plus_count++;
  202.     ieee_write_byte(abfd, ieee_variable_R_enum);
  203.     ieee_write_byte(abfd, section->index  +IEEE_SECTION_NUMBER_BASE);
  204.   }
  205.  
  206.  
  207.  
  208.   if (symbol != (asymbol *)NULL) {
  209.     plus_count++;
  210.     if ((symbol->flags & BSF_UNDEFINED ) ||
  211.     (symbol->flags & BSF_FORT_COMM)) {
  212.       ieee_write_byte(abfd, ieee_variable_X_enum);
  213.       ieee_write_int(abfd, symbol->value);
  214.     }
  215.     else if (symbol->flags & BSF_GLOBAL) {
  216.       ieee_write_byte(abfd, ieee_variable_I_enum);
  217.       ieee_write_int(abfd, symbol->value);
  218.     }
  219.     else if (symbol->flags & BSF_LOCAL) {
  220.       /* This is a reference to a defined local symbol, 
  221.      We can easily do a local as a section+offset */
  222.       if (symbol->section != (asection *)NULL) {
  223.     /* If this symbol is not absolute, add the base of it */
  224.     ieee_write_byte(abfd, ieee_variable_R_enum); /* or L */
  225.     ieee_write_byte(abfd, symbol->section->index +
  226.             IEEE_SECTION_NUMBER_BASE);
  227.     plus_count++;
  228.       }
  229.       
  230.       ieee_write_int(abfd, symbol->value);
  231.     }
  232.     else {
  233.  
  234.       BFD_FAIL();
  235.     }
  236.   }
  237.  
  238.   if(pcrel) {
  239.     /* subtract the pc from here by asking for PC of this section*/
  240.     ieee_write_byte(abfd, ieee_variable_P_enum);
  241.     ieee_write_byte(abfd, index  +IEEE_SECTION_NUMBER_BASE);
  242.     ieee_write_byte(abfd, ieee_function_minus_enum);
  243.   }
  244.  
  245.   if (value != 0){
  246.     while (plus_count > 0) {
  247.       ieee_write_byte(abfd, ieee_function_plus_enum);
  248.       plus_count--;
  249.     }
  250.   }
  251.   else {
  252.     if (!plus_count)
  253.       ieee_write_byte(abfd,0);
  254.   }
  255.  
  256. }
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266. /*****************************************************************************/
  267.  
  268. /*
  269. writes any integer into the buffer supplied and always takes 5 bytes
  270. */
  271. static void
  272. DEFUN(ieee_write_int5,(buffer, value),
  273.       bfd_byte*buffer AND
  274.       bfd_vma value )
  275. {
  276.   buffer[0] = (bfd_byte)ieee_number_repeat_4_enum;
  277.   buffer[1] = (value >> 24 ) & 0xff;
  278.   buffer[2] = (value >> 16 ) & 0xff;
  279.   buffer[3] = (value >> 8 ) & 0xff;
  280.   buffer[4] = (value >> 0 ) & 0xff;
  281. }
  282. static void
  283. DEFUN(ieee_write_int5_out, (abfd, value),
  284.       bfd *abfd AND
  285.       bfd_vma value)
  286. {
  287.   bfd_byte b[5];
  288.   ieee_write_int5(b, value);
  289.   bfd_write((PTR)b,1,5,abfd);
  290. }
  291.  
  292.  
  293. static boolean 
  294. DEFUN(parse_int,(ieee, value_ptr),
  295.       common_header_type  *ieee AND
  296.       bfd_vma *value_ptr)
  297. {
  298.   int value = this_byte(ieee);
  299.   int result;
  300.   if (value >= 0 && value <= 127) {
  301.     *value_ptr = value;
  302.     next_byte(ieee);
  303.     return true;
  304.   } 
  305.   else if (value >= 0x80 && value <= 0x88) {
  306.     unsigned int count = value & 0xf;
  307.     result = 0;
  308.     next_byte(ieee);
  309.     while (count) {
  310.       result =(result << 8) | this_byte_and_next(ieee);
  311.       count--;
  312.     }
  313.     *value_ptr = result;
  314.     return true;
  315.   } 
  316.   return false;
  317. }
  318. static int
  319. DEFUN(parse_i,(ieee, ok),
  320.    common_header_type *ieee AND
  321.       boolean *ok)
  322. {
  323.   bfd_vma x;
  324.   *ok = parse_int(ieee, &x);
  325.   return x;
  326. }
  327.  
  328. static bfd_vma 
  329. DEFUN(must_parse_int,(ieee),
  330.      common_header_type *ieee)
  331. {
  332.   bfd_vma result;
  333.   BFD_ASSERT(parse_int(ieee, &result) == true);
  334.   return result;
  335. }
  336.  
  337. typedef struct 
  338. {
  339.   bfd_vma value;
  340.   asection *section;
  341.   ieee_symbol_index_type symbol;
  342. } ieee_value_type;
  343.  
  344.  
  345. static 
  346. reloc_howto_type abs32_howto 
  347.  = HOWTO(1,0,2,32,false,0,false,true,0,"abs32",true,0xffffffff, 0xffffffff,false);
  348. static
  349. reloc_howto_type abs16_howto 
  350.  = HOWTO(1,0,1,16,false,0,false,true,0,"abs16",true,0x0000ffff, 0x0000ffff,false);
  351.  
  352. static
  353. reloc_howto_type abs8_howto 
  354.  = HOWTO(1,0,0,8,false,0,false,true,0,"abs8",true,0x000000ff, 0x000000ff,false);
  355.  
  356. static 
  357. reloc_howto_type rel32_howto 
  358.  = HOWTO(1,0,2,32,true,0,false,true,0,"rel32",true,0xffffffff, 0xffffffff,true);
  359. static
  360. reloc_howto_type rel16_howto 
  361.  = HOWTO(1,0,1,16,true,0,false,true,0,"rel16",true,0x0000ffff, 0x0000ffff,true);
  362.  
  363. static
  364. reloc_howto_type rel8_howto 
  365.  = HOWTO(1,0,0,8,true,0,false,true,0,"rel8",true,0x000000ff, 0x000000ff,true);
  366.  
  367.  
  368. static ieee_symbol_index_type NOSYMBOL = {  0, 0};
  369.  
  370.  
  371. static void
  372. DEFUN(parse_expression,(ieee, value, section, symbol, pcrel, extra),
  373.       ieee_data_type *ieee AND
  374.       bfd_vma *value AND
  375.       asection **section AND
  376.       ieee_symbol_index_type *symbol AND
  377.       boolean *pcrel AND
  378.       unsigned int *extra)
  379.  
  380. {
  381. #define POS sp[1]
  382. #define TOS sp[0]
  383. #define NOS sp[-1]
  384. #define INC sp++;
  385. #define DEC sp--;
  386.  
  387.   boolean loop = true;
  388.   ieee_value_type stack[10];
  389.  
  390.   /* The stack pointer always points to the next unused location */
  391. #define PUSH(x,y,z) TOS.symbol=x;TOS.section=y;TOS.value=z;INC;
  392. #define POP(x,y,z) DEC;x=TOS.symbol;y=TOS.section;z=TOS.value;
  393.   ieee_value_type *sp = stack;
  394.  
  395.   while (loop) {
  396.     switch (this_byte(&(ieee->h))) 
  397.     {
  398.     case ieee_variable_P_enum:
  399.       /* P variable, current program counter for section n */
  400.         {
  401.           int section_n ;
  402.           next_byte(&(ieee->h));
  403.           *pcrel = true;
  404.           section_n  = must_parse_int(&(ieee->h));
  405.           PUSH(NOSYMBOL, 0,
  406.            TOS.value = ieee->section_table[section_n]->vma +
  407.            ieee_per_section(ieee->section_table[section_n])->pc);
  408.           break;
  409.         }
  410.     case ieee_variable_L_enum:
  411.       /* L variable  address of section N */
  412.       next_byte(&(ieee->h));
  413.       PUSH(NOSYMBOL,ieee->section_table[must_parse_int(&(ieee->h))],0);
  414.       break;
  415.     case ieee_variable_R_enum:
  416.       /* R variable, logical address of section module */
  417.       /* FIXME, this should be different to L */
  418.       next_byte(&(ieee->h));
  419.       PUSH(NOSYMBOL,ieee->section_table[must_parse_int(&(ieee->h))],0);
  420.       break;
  421.     case ieee_variable_S_enum:
  422.       /* S variable, size in MAUS of section module */
  423.       next_byte(&(ieee->h));
  424.       PUSH(NOSYMBOL,
  425.            0,
  426.            ieee->section_table[must_parse_int(&(ieee->h))]->size);
  427.       break;
  428.       case ieee_variable_I_enum:
  429.     case ieee_variable_X_enum:
  430.       /* Push the address of external variable n */
  431.         {
  432.           ieee_symbol_index_type sy;
  433.           next_byte(&(ieee->h));
  434.           sy.index  = (int)(must_parse_int(&(ieee->h))) ;
  435.           sy.letter = 'X';
  436.  
  437.           PUSH(sy, 0, 0);
  438.         }    
  439.       break;
  440.     case ieee_function_minus_enum:
  441.         {
  442.           bfd_vma value1, value2;
  443.           asection *section1, *section_dummy;
  444.           ieee_symbol_index_type sy;
  445.           next_byte(&(ieee->h));
  446.  
  447.           POP(sy, section1, value1);
  448.           POP(sy, section_dummy, value2);
  449.           PUSH(sy, section1 ? section1 : section_dummy, value1-value2);
  450.         }
  451.       break;
  452.     case ieee_function_plus_enum:
  453.         {
  454.           bfd_vma value1, value2;
  455.           asection *section1;
  456.           asection *section2;
  457.           ieee_symbol_index_type sy1;
  458.           ieee_symbol_index_type sy2;
  459.           next_byte(&(ieee->h));
  460.  
  461.           POP(sy1, section1, value1);
  462.           POP(sy2, section2, value2);
  463.           PUSH(sy1.letter ? sy1 : sy2, section1 ? section1: section2, value1+value2);
  464.         }
  465.       break;
  466.     default: 
  467.         {
  468.           bfd_vma va;
  469.           BFD_ASSERT(this_byte(&(ieee->h)) < (int)ieee_variable_A_enum 
  470.              || this_byte(&(ieee->h)) > (int)ieee_variable_Z_enum);
  471.           if (parse_int(&(ieee->h), &va)) 
  472.           {
  473.             PUSH(NOSYMBOL,0, va);
  474.           }
  475.           else {
  476.         /* 
  477.           Thats all that we can understand. As far as I can see
  478.           there is a bug in the Microtec IEEE output which I'm
  479.           using to scan, whereby the comma operator is ommited
  480.           sometimes in an expression, giving expressions with too
  481.           many terms. We can tell if that's the case by ensuring
  482.           that sp == stack here. If not, then we've pushed
  483.           something too far, so we keep adding
  484.           */
  485.  
  486.         while (sp != stack+1) {
  487.           asection *section1;
  488.           ieee_symbol_index_type sy1;
  489.           POP(sy1, section1, *extra);
  490.         }
  491.  
  492.         POP(*symbol, *section, *value);
  493.         loop = false;
  494.           }
  495.         }
  496.  
  497.     }
  498.   }
  499. }
  500.  
  501.  
  502.  
  503. #define ieee_seek(abfd, offset) \
  504.   ieee_data(abfd)->h.input_p = ieee_data(abfd)->h.first_byte + offset
  505.  
  506. #define ieee_pos(abfd)   ieee_data(abfd)->h.input_p -ieee_data(abfd)->h.first_byte 
  507.  
  508. static unsigned int last_index;
  509.  
  510. static ieee_symbol_type *
  511. DEFUN(get_symbol,(abfd, 
  512.           ieee,  
  513.           last_symbol,
  514.           symbol_count,
  515. pptr,
  516. max_index
  517.           ),
  518.       bfd *abfd AND
  519.       ieee_data_type *ieee AND
  520.       ieee_symbol_type *last_symbol AND
  521.       unsigned int *symbol_count AND
  522.           ieee_symbol_type *** pptr AND
  523.       unsigned int *max_index
  524.       )
  525. {
  526.   /* Need a new symbol */
  527.   unsigned int new_index = must_parse_int(&(ieee->h));
  528.   if (new_index != last_index) {
  529.     ieee_symbol_type  *   new_symbol = (ieee_symbol_type *)bfd_alloc(ieee->h.abfd,
  530.                                      sizeof(ieee_symbol_type));
  531.  
  532.     new_symbol->index = new_index;
  533.     last_index = new_index;
  534.     ( *symbol_count)++;    
  535.     ** pptr= new_symbol;
  536.     *pptr = &new_symbol->next;
  537.     if (new_index > *max_index) {
  538.       *max_index = new_index;
  539.     }
  540.     return new_symbol;
  541.   }
  542.   return last_symbol;
  543. }
  544. static void
  545. DEFUN(ieee_slurp_external_symbols,(abfd),
  546.       bfd *abfd)
  547. {
  548.   ieee_data_type *ieee = ieee_data(abfd);
  549.   file_ptr offset = ieee->w.r.external_part;
  550.  
  551.  
  552.   ieee_symbol_type **prev_symbols_ptr = &ieee->external_symbols;
  553.   ieee_symbol_type **prev_reference_ptr = &ieee->external_reference;
  554.   ieee_symbol_type  *symbol = (ieee_symbol_type *)NULL;
  555.   unsigned int symbol_count = 0;
  556.   boolean loop = true;
  557.   last_index = 0xffffff;
  558.   ieee->symbol_table_full = true;
  559.  
  560.   ieee_seek(abfd, offset );
  561.  
  562.   while (loop) {
  563.     switch (this_byte(&(ieee->h))) {
  564.     case ieee_nn_record:
  565.       next_byte(&(ieee->h));
  566.       symbol = get_symbol(abfd, ieee, symbol, &symbol_count,
  567.               &prev_symbols_ptr, 
  568.               &ieee->external_symbol_max_index);
  569.  
  570.       symbol->symbol.the_bfd = abfd;
  571.       symbol->symbol.name = read_id(&(ieee->h));
  572.       symbol->symbol.udata = (PTR)NULL;
  573.       symbol->symbol.flags = BSF_NO_FLAGS;
  574.  
  575.       
  576.       break;
  577.     case ieee_external_symbol_enum:
  578.       next_byte(&(ieee->h));
  579.                          
  580.       symbol = get_symbol(abfd, ieee, symbol, &symbol_count,
  581.               &prev_symbols_ptr,
  582.               &ieee->external_symbol_max_index);
  583.  
  584.  
  585.       BFD_ASSERT (symbol->index >= ieee->external_symbol_min_index);
  586.  
  587.       symbol->symbol.the_bfd = abfd;
  588.       symbol->symbol.name = read_id(&(ieee->h));
  589.       symbol->symbol.udata = (PTR)NULL;
  590.       symbol->symbol.flags = BSF_NO_FLAGS;
  591.       break;
  592.     case ieee_attribute_record_enum >> 8:
  593.     {
  594.       unsigned int symbol_name_index;
  595.       unsigned int symbol_type_index;
  596.       unsigned int symbol_attribute_def;
  597.       bfd_vma value;
  598.       next_byte(&(ieee->h)); /* Skip prefix */
  599.       next_byte(&(ieee->h));
  600.       symbol_name_index = must_parse_int(&(ieee->h));
  601.       symbol_type_index = must_parse_int(&(ieee->h));
  602.       symbol_attribute_def = must_parse_int(&(ieee->h));
  603.       switch (symbol_attribute_def) {
  604.       case 63:
  605.         /* Module misc; followed by two fields which describe the
  606.            current module block. The first fired is the type id
  607.            number, the second is the number of asn records
  608.            associated with the directive */
  609.         parse_int(&(ieee->h),&value);
  610.         parse_int(&(ieee->h),&value);
  611.         break;
  612.  
  613.       default:
  614.         parse_int(&(ieee->h),&value);
  615.         break;
  616.       }
  617.     }
  618.       break;
  619.     case ieee_value_record_enum >> 8:
  620.     {
  621.       unsigned int symbol_name_index;
  622.       ieee_symbol_index_type symbol_ignore;
  623.       boolean pcrel_ignore;
  624.       unsigned int extra;
  625.       next_byte(&(ieee->h));
  626.       next_byte(&(ieee->h));
  627.  
  628.       symbol_name_index = must_parse_int(&(ieee->h));
  629.       parse_expression(ieee,
  630.                &symbol->symbol.value,
  631.                &symbol->symbol.section,
  632.                &symbol_ignore, 
  633.                &pcrel_ignore, 
  634.                &extra);
  635.       if (symbol->symbol.section != (asection *)NULL) {
  636.         symbol->symbol.flags  = BSF_GLOBAL | BSF_EXPORT;
  637.       }
  638.       else {
  639.         symbol->symbol.flags  = BSF_GLOBAL | BSF_EXPORT | BSF_ABSOLUTE;
  640.       }
  641.     }
  642.       break;
  643.     case ieee_weak_external_reference_enum:
  644.     { bfd_vma size;
  645.       bfd_vma value ;
  646.       next_byte(&(ieee->h));
  647.       /* Throw away the external reference index */
  648.       (void)must_parse_int(&(ieee->h));
  649.       /* Fetch the default size if not resolved */
  650.       size = must_parse_int(&(ieee->h));
  651.       /* Fetch the defautlt value if available */
  652.       if (  parse_int(&(ieee->h), &value) == false) {
  653.         value = 0;
  654.       }
  655.       /* This turns into a common */
  656.       symbol->symbol.flags = BSF_FORT_COMM;
  657.       symbol->symbol.value = size;
  658.     }
  659.       break;
  660.  
  661.     case ieee_external_reference_enum: 
  662.       next_byte(&(ieee->h));
  663.  
  664.       symbol = get_symbol(abfd, ieee, symbol, &symbol_count,
  665.               &prev_reference_ptr,
  666.               &ieee->external_reference_max_index);
  667.  
  668.  
  669.       symbol->symbol.the_bfd = abfd;
  670.       symbol->symbol.name = read_id(&(ieee->h));
  671.       symbol->symbol.udata = (PTR)NULL;
  672.       symbol->symbol.section = (asection *)NULL;
  673.       symbol->symbol.value = (bfd_vma)0;
  674.       symbol->symbol.flags = BSF_UNDEFINED;
  675.  
  676.       BFD_ASSERT (symbol->index >= ieee->external_reference_min_index);
  677.       break;
  678.  
  679.     default:
  680.       loop = false;
  681.     }
  682.   }
  683.  
  684.   if (ieee->external_symbol_max_index != 0) {
  685.     ieee->external_symbol_count = 
  686.       ieee->external_symbol_max_index -
  687.     ieee->external_symbol_min_index + 1  ;
  688.   }
  689.   else  {
  690.     ieee->external_symbol_count = 0;
  691.   }
  692.  
  693.  
  694.   if(ieee->external_reference_max_index != 0) {
  695.     ieee->external_reference_count = 
  696.       ieee->external_reference_max_index -
  697.     ieee->external_reference_min_index + 1;
  698.   }
  699.   else {
  700.     ieee->external_reference_count = 0;
  701.   }
  702.  
  703.   abfd->symcount =
  704.     ieee->external_reference_count +  ieee->external_symbol_count;
  705.  
  706.   if (symbol_count != abfd->symcount) {
  707.     /* There are gaps in the table -- */
  708.     ieee->symbol_table_full = false;
  709.   }
  710.  
  711.  
  712.   *prev_symbols_ptr = (ieee_symbol_type *)NULL;
  713.   *prev_reference_ptr = (ieee_symbol_type *)NULL;
  714. }
  715.  
  716. static void
  717. DEFUN(ieee_slurp_symbol_table,(abfd),
  718.       bfd *abfd)
  719. {
  720.   if (ieee_data(abfd)->read_symbols == false) {
  721.     ieee_slurp_external_symbols(abfd);
  722.     ieee_data(abfd)->read_symbols= true;
  723.   }
  724. }
  725.  
  726. unsigned int
  727. DEFUN(ieee_get_symtab_upper_bound,(abfd),
  728.       bfd *abfd)
  729. {
  730.   ieee_slurp_symbol_table (abfd);
  731.  
  732.   return (abfd->symcount != 0) ? 
  733.     (abfd->symcount+1) * (sizeof (ieee_symbol_type *)) : 0;
  734. }
  735.  
  736. /* 
  737. Move from our internal lists to the canon table, and insert in
  738. symbol index order
  739. */
  740.  
  741. extern bfd_target ieee_vec;
  742. unsigned int
  743. DEFUN(ieee_get_symtab,(abfd, location),
  744.       bfd *abfd AND
  745.       asymbol **location)
  746. {
  747.   ieee_symbol_type *symp;
  748.   static bfd dummy_bfd;
  749.   static asymbol empty_symbol =
  750.     { &dummy_bfd," ieee empty",(symvalue)0,BSF_DEBUGGING | BSF_ABSOLUTE};
  751.  
  752. if (abfd->symcount) {
  753.  
  754.  
  755.  
  756.  
  757.   ieee_data_type *ieee = ieee_data(abfd);
  758.   dummy_bfd.xvec= &ieee_vec;
  759.   ieee_slurp_symbol_table(abfd);
  760.  
  761.   if (ieee->symbol_table_full == false) {
  762.     /* Arrgh - there are gaps in the table, run through and fill them */
  763.     /* up with pointers to a null place */
  764.     unsigned int i;
  765.     for (i= 0; i < abfd->symcount; i++) {
  766.       location[i] = &empty_symbol;
  767.     }
  768.   }
  769.  
  770.  
  771.   ieee->external_symbol_base_offset= -  ieee->external_symbol_min_index;
  772.   for (symp = ieee_data(abfd)->external_symbols;
  773.        symp != (ieee_symbol_type *)NULL;
  774.        symp = symp->next) {
  775.     /* Place into table at correct index locations */
  776.     location[symp->index + ieee->external_symbol_base_offset] = &symp->symbol;
  777.  
  778.   }
  779.  
  780.   /* The external refs are indexed in a bit */
  781.   ieee->external_reference_base_offset   =
  782.     -  ieee->external_reference_min_index +ieee->external_symbol_count ;
  783.  
  784.   for (symp = ieee_data(abfd)->external_reference;
  785.        symp != (ieee_symbol_type *)NULL;
  786.        symp = symp->next) {
  787.     location[symp->index + ieee->external_reference_base_offset] =
  788.       &symp->symbol;
  789.  
  790.   }
  791.  
  792.  
  793.  
  794.   location[abfd->symcount] = (asymbol *)NULL;
  795. }
  796.   return abfd->symcount;
  797. }
  798. static asection *
  799. DEFUN(get_section_entry,(abfd, ieee,index),
  800.  bfd *abfd AND
  801.      ieee_data_type *ieee AND
  802.       unsigned int index)
  803. {
  804.   if (ieee->section_table[index] == (asection *)NULL) {
  805.     asection *section = bfd_make_section(abfd, " tempname");
  806.     ieee->section_table[index] = section;
  807.     section->flags = SEC_NO_FLAGS;
  808.     section->target_index = index;
  809.     ieee->section_table[index] = section;
  810.   }
  811.   return ieee->section_table[index];
  812. }
  813.  
  814. static void
  815. DEFUN(ieee_slurp_sections,(abfd),
  816.       bfd *abfd)
  817. {
  818.   ieee_data_type *ieee = ieee_data(abfd);
  819.   file_ptr offset = ieee->w.r.section_part;
  820.  
  821.   asection *section = (asection *)NULL;
  822.  
  823.   if (offset != 0) {
  824.     bfd_byte section_type[3];
  825.     ieee_seek(abfd, offset);
  826.     while (true) {
  827.       switch (this_byte(&(ieee->h))) {
  828.       case ieee_section_type_enum:
  829.       {
  830.         unsigned int section_index ;
  831.         next_byte(&(ieee->h));
  832.         section_index = must_parse_int(&(ieee->h));
  833.         /* Fixme to be nice about a silly number of sections */
  834.         BFD_ASSERT(section_index < NSECTIONS);
  835.  
  836.         section =get_section_entry(abfd, ieee, section_index);
  837.  
  838.         section_type[0] =  this_byte_and_next(&(ieee->h));
  839.         switch (section_type[0]) {
  840.         case 0xC1:
  841.           /* Normal attributes for absolute sections    */
  842.           section_type[1] = this_byte(&(ieee->h));
  843.           section->flags = SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
  844.           switch(section_type[1]) {
  845.           case 0xD3:
  846.         next_byte(&(ieee->h));
  847.         section_type[2] = this_byte(&(ieee->h));
  848.         switch (section_type[2]) 
  849.             {
  850.             case 0xD0:
  851.               /* Normal code */
  852.               next_byte(&(ieee->h));
  853.               section->flags |= SEC_LOAD | SEC_CODE;
  854.               break;
  855.             case 0xC4:
  856.               next_byte(&(ieee->h));
  857.               section->flags |= SEC_LOAD  | SEC_DATA;
  858.               /* Normal data */
  859.               break;
  860.             case 0xD2:
  861.               next_byte(&(ieee->h));
  862.               /* Normal rom data */
  863.               section->flags |= SEC_LOAD | SEC_ROM | SEC_DATA;
  864.               break;
  865.             default:
  866.               break;
  867.             }
  868.           }
  869.           break;
  870.         case 0xC3:
  871.           section_type[1] = this_byte(&(ieee->h));
  872.           section->flags = SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
  873.           switch (section_type[1]) {
  874.           case 0xD0:
  875.         /* Normal code */
  876.         next_byte(&(ieee->h));
  877.         section->flags |= SEC_LOAD | SEC_CODE;
  878.         break;
  879.           case 0xC4:
  880.         next_byte(&(ieee->h));
  881.         section->flags |= SEC_LOAD  | SEC_DATA;
  882.         /* Normal data */
  883.         break;
  884.           case 0xD2:
  885.         next_byte(&(ieee->h));
  886.         /* Normal rom data */
  887.         section->flags |= SEC_LOAD | SEC_ROM | SEC_DATA;
  888.         break;
  889.           default:
  890.         break;
  891.           }
  892.         }
  893.         section->name = read_id(&(ieee->h));
  894.           { bfd_vma parent, brother, context;
  895.         parse_int(&(ieee->h), &parent);
  896.         parse_int(&(ieee->h), &brother);
  897.         parse_int(&(ieee->h), &context);
  898.           }
  899.  
  900.  
  901.       }
  902.     break;
  903.       case ieee_section_alignment_enum:
  904.       { 
  905.         unsigned int section_index;
  906.         bfd_vma value;
  907.         asection *section;
  908.         next_byte(&(ieee->h));
  909.         section_index = must_parse_int(&ieee->h);
  910.         section = get_section_entry(abfd, ieee, section_index);
  911.         if (section_index > ieee->section_count) {
  912.           ieee->section_count = section_index;
  913.         }
  914.         section->alignment_power =
  915.           bfd_log2(must_parse_int(&ieee->h));
  916.         (void)parse_int(&(ieee->h), & value);
  917.       }
  918.     break;
  919.       case ieee_e2_first_byte_enum: 
  920.       {
  921.         ieee_record_enum_type t = (ieee_record_enum_type)(read_2bytes(&(ieee->h)));
  922.  
  923.         switch (t) {
  924.         case ieee_section_size_enum:
  925.           section = ieee->section_table[must_parse_int(&(ieee->h))];
  926.           section->size = must_parse_int(&(ieee->h));
  927.           break;
  928.         case ieee_physical_region_size_enum:
  929.           section = ieee->section_table[must_parse_int(&(ieee->h))];
  930.           section->size = must_parse_int(&(ieee->h));
  931.           break;
  932.         case ieee_region_base_address_enum:
  933.           section = ieee->section_table[must_parse_int(&(ieee->h))];
  934.           section->vma = must_parse_int(&(ieee->h));
  935.           break;
  936.         case ieee_mau_size_enum:
  937.           must_parse_int(&(ieee->h));
  938.           must_parse_int(&(ieee->h));
  939.           break;
  940.         case ieee_m_value_enum:
  941.           must_parse_int(&(ieee->h));
  942.           must_parse_int(&(ieee->h));
  943.           break;
  944.         case ieee_section_base_address_enum:
  945.           section = ieee->section_table[must_parse_int(&(ieee->h))];
  946.           section->vma = must_parse_int(&(ieee->h));
  947.           break;
  948.         case ieee_section_offset_enum:
  949.           (void) must_parse_int(&(ieee->h));
  950.           (void) must_parse_int(&(ieee->h));
  951.           break;
  952.         default:
  953.           return;
  954.         }
  955.       }
  956.     break;
  957.       default:
  958.     return;
  959.       }
  960.     }
  961.   }
  962. }
  963.  
  964. /***********************************************************************
  965. *  archive stuff 
  966. */
  967. bfd_target *
  968. DEFUN(ieee_archive_p,(abfd),
  969.       bfd *abfd)
  970. {
  971.   char *library;
  972.   boolean loop;
  973.  
  974.   unsigned int i;
  975. uint8e_type buffer[512];
  976.  
  977.   int buffer_offset = 0;
  978.   ieee_ar_data_type *save = ieee_ar_data(abfd);
  979.   ieee_ar_data_type *ieee ;
  980.   set_tdata(abfd, bfd_alloc(abfd, sizeof(ieee_ar_data_type)));
  981.   ieee=  ieee_ar_data(abfd);
  982.  
  983.  
  984.   bfd_read((PTR)buffer, 1, sizeof(buffer), abfd);
  985.  
  986.   ieee->h.first_byte = buffer;
  987.   ieee->h.input_p = buffer;
  988.  
  989.   ieee->h.abfd = abfd;
  990.  
  991.   if (this_byte(&(ieee->h)) != Module_Beginning) return (bfd_target*)NULL;
  992.  
  993.   next_byte(&(ieee->h));
  994.   library= read_id(&(ieee->h));
  995.   if (strcmp(library , "LIBRARY") != 0) {
  996.     bfd_release(abfd, ieee);
  997.     set_tdata (abfd, save);
  998.     return (bfd_target *)NULL;
  999.   }
  1000.   /* Throw away the filename */
  1001.   free( read_id(&(ieee->h)));
  1002.   /* This must be an IEEE archive, so we'll buy some space to do
  1003.      things */
  1004.   ieee->element_count = 0;
  1005.   ieee->element_index = 0;
  1006.  
  1007.   next_byte(&(ieee->h));    /* Drop the ad part */
  1008.   must_parse_int(&(ieee->h));    /* And the two dummy numbers */
  1009.   must_parse_int(&(ieee->h));
  1010.  
  1011.   loop = true;
  1012.   /* Read the index of the BB table */
  1013.   while (loop) {
  1014.     ieee_ar_obstack_type t; 
  1015.     int rec =read_2bytes(&(ieee->h));
  1016.     if (rec ==(int)ieee_assign_value_to_variable_enum) {
  1017.       int record_number = must_parse_int(&(ieee->h));
  1018.       t.file_offset = must_parse_int(&(ieee->h));
  1019.       t.abfd = (bfd *)NULL;
  1020.       ieee->element_count++;
  1021.       bfd_alloc_grow(abfd, (PTR)&t, sizeof(t));
  1022.  
  1023.       /* Make sure that we don't go over the end of the buffer */
  1024.  
  1025.       if (ieee_pos(abfd) > sizeof(buffer)/2) {
  1026.     /* Past half way, reseek and reprime */
  1027.     buffer_offset += ieee_pos(abfd);
  1028.     bfd_seek(abfd, buffer_offset, SEEK_SET);
  1029.     bfd_read((PTR)buffer, 1, sizeof(buffer), abfd);
  1030.     ieee->h.first_byte = buffer;
  1031.     ieee->h.input_p = buffer;
  1032.       }
  1033.     }
  1034.     else loop = false;
  1035.   }
  1036.  
  1037.   ieee->elements = (ieee_ar_obstack_type *)bfd_alloc_finish(abfd);
  1038.  
  1039.   /* Now scan the area again, and replace BB offsets with file */
  1040.   /* offsets */
  1041.  
  1042.  
  1043.   for (i = 2; i < ieee->element_count; i++) {
  1044.     bfd_seek(abfd, ieee->elements[i].file_offset, SEEK_SET);
  1045.     bfd_read((PTR)buffer, 1, sizeof(buffer), abfd);
  1046.     ieee->h.first_byte = buffer;
  1047.     ieee->h.input_p = buffer;
  1048.     
  1049.     next_byte(&(ieee->h));    /* Drop F8 */
  1050.     next_byte(&(ieee->h));    /* Drop 14 */
  1051.     must_parse_int(&(ieee->h));    /* Drop size of block */
  1052.     if (must_parse_int(&(ieee->h)) != 0) {
  1053.       /* This object has been deleted */
  1054.       ieee->elements[i].file_offset = 0;
  1055.     }
  1056.     else {
  1057.       ieee->elements[i].file_offset = must_parse_int(&(ieee->h));
  1058.     }
  1059.   }
  1060.  
  1061.   return abfd->xvec;
  1062.  
  1063. }
  1064.  
  1065. static boolean
  1066. DEFUN(ieee_mkobject,(abfd),
  1067.       bfd *abfd)
  1068.   set_tdata (abfd, bfd_zalloc(abfd,sizeof(ieee_data_type)));
  1069.   
  1070.  
  1071.   return true;
  1072. }
  1073.  
  1074. bfd_target *
  1075. DEFUN(ieee_object_p,(abfd),
  1076.       bfd *abfd)
  1077. {
  1078.   char *processor;
  1079.   unsigned int part;
  1080.   ieee_data_type *ieee;
  1081.   uint8e_type buffer[300];
  1082.   ieee_data_type *save = ieee_data(abfd);
  1083.   set_tdata (abfd, 0);
  1084.   ieee_mkobject(abfd);
  1085.   ieee = ieee_data(abfd);
  1086.   
  1087.   /* Read the first few bytes in to see if it makes sense */
  1088.   bfd_read((PTR)buffer, 1, sizeof(buffer), abfd);
  1089.  
  1090.   ieee->h.input_p = buffer;
  1091.   if (this_byte_and_next(&(ieee->h)) != Module_Beginning) goto fail;
  1092.  
  1093.   ieee->read_symbols= false;
  1094.   ieee->read_data= false;
  1095.   ieee->section_count = 0;
  1096.   ieee->external_symbol_max_index = 0;
  1097.   ieee->external_symbol_min_index = IEEE_PUBLIC_BASE;
  1098.   ieee->external_reference_min_index =IEEE_REFERENCE_BASE;
  1099.   ieee->external_reference_max_index = 0;
  1100.   ieee->h.abfd = abfd;
  1101.   memset((PTR)ieee->section_table, 0,     sizeof(ieee->section_table));
  1102.  
  1103.   processor = ieee->mb.processor = read_id(&(ieee->h));
  1104.   if (strcmp(processor,"LIBRARY") == 0) goto fail;
  1105.   ieee->mb.module_name = read_id(&(ieee->h));
  1106.   if (abfd->filename == (char *)NULL) {
  1107.     abfd->filename =  ieee->mb.module_name;
  1108.   }
  1109.   /* Determine the architecture and machine type of the object file.
  1110.      */
  1111.     {
  1112.       bfd_arch_info_type *arch = bfd_scan_arch(processor);
  1113.       if (arch == 0) goto fail;
  1114.       abfd->arch_info = arch;
  1115.     }
  1116.  
  1117.   if (this_byte(&(ieee->h)) != (int)ieee_address_descriptor_enum) {
  1118.     goto fail;
  1119.   }
  1120.   next_byte(&(ieee->h));    
  1121.  
  1122.   if (parse_int(&(ieee->h), &ieee->ad.number_of_bits_mau) == false) {
  1123.     goto fail;
  1124.   }
  1125.   if(parse_int(&(ieee->h), &ieee->ad.number_of_maus_in_address) == false) {
  1126.     goto fail;
  1127.   }
  1128.  
  1129.   /* If there is a byte order info, take it */
  1130.   if (this_byte(&(ieee->h)) == (int)ieee_variable_L_enum ||
  1131.       this_byte(&(ieee->h)) == (int)ieee_variable_M_enum)
  1132.     next_byte(&(ieee->h));
  1133.  
  1134.  
  1135.   for (part = 0; part < N_W_VARIABLES; part++) {
  1136.     boolean ok;
  1137.     if (read_2bytes(&(ieee->h)) != (int) ieee_assign_value_to_variable_enum) {
  1138.       goto fail;
  1139.     }
  1140.     if (this_byte_and_next(&(ieee->h)) != part)  {
  1141.       goto fail;
  1142.     }
  1143.  
  1144.     ieee->w.offset[part] = parse_i(&(ieee->h), &ok);
  1145.     if (ok==false) {
  1146.       goto fail;
  1147.     }
  1148.  
  1149.   }
  1150.   abfd->flags = HAS_SYMS;
  1151.  
  1152. /* By now we know that this is a real IEEE file, we're going to read
  1153.    the whole thing into memory so that we can run up and down it
  1154.    quickly. We can work out how big the file is from the trailer
  1155.    record */
  1156.  
  1157.   ieee_data(abfd)->h.first_byte = (uint8e_type *) bfd_alloc(ieee->h.abfd, ieee->w.r.me_record
  1158.                         + 50);
  1159.   bfd_seek(abfd, 0, 0);
  1160.   bfd_read((PTR)(ieee_data(abfd)->h.first_byte), 1,   ieee->w.r.me_record+50,  abfd);
  1161.  
  1162.   ieee_slurp_sections(abfd);
  1163.   return abfd->xvec;
  1164.  fail:
  1165.   (void)  bfd_release(abfd, ieee);
  1166.   set_tdata (abfd, save);
  1167.   return (bfd_target *)NULL;
  1168. }
  1169.  
  1170.  
  1171. void 
  1172. DEFUN(ieee_print_symbol,(ignore_abfd, afile,  symbol, how),
  1173.       bfd *ignore_abfd AND
  1174.       PTR afile AND
  1175.       asymbol *symbol AND
  1176.       bfd_print_symbol_type how)
  1177. {
  1178.   FILE *file = (FILE *)afile;
  1179.  
  1180.   switch (how) {
  1181.   case bfd_print_symbol_name:
  1182.     fprintf(file,"%s", symbol->name);
  1183.     break;
  1184.   case bfd_print_symbol_more:
  1185. #if 0
  1186.     fprintf(file,"%4x %2x",aout_symbol(symbol)->desc & 0xffff,
  1187.         aout_symbol(symbol)->other  & 0xff);
  1188. #endif
  1189.     BFD_FAIL();
  1190.     break;
  1191.   case bfd_print_symbol_all:
  1192.       {
  1193.     CONST char *section_name = symbol->section == (asection *)NULL ?
  1194.       "*abs" : symbol->section->name;
  1195.     if (symbol->name[0] == ' ') {
  1196.       fprintf(file,"* empty table entry ");
  1197.     }
  1198.     else {
  1199.       bfd_print_symbol_vandf((PTR)file,symbol);
  1200.  
  1201.       fprintf(file," %-5s %04x %02x %s",
  1202.           section_name,
  1203.           (unsigned)          ieee_symbol(symbol)->index,
  1204.           (unsigned)          0, /*
  1205.                        aout_symbol(symbol)->desc & 0xffff,
  1206.                        aout_symbol(symbol)->other  & 0xff,*/
  1207.           symbol->name);
  1208.     }
  1209.       }
  1210.     break;
  1211.   }
  1212. }
  1213.  
  1214.  
  1215. static void
  1216. DEFUN(do_one,(ieee, current_map, location_ptr,s),
  1217.       ieee_data_type *ieee AND
  1218.       ieee_per_section_type *current_map AND
  1219.       uint8e_type *location_ptr AND
  1220.       asection *s)
  1221. {
  1222.   switch (this_byte(&(ieee->h)))  
  1223.       {
  1224.       case ieee_load_constant_bytes_enum:
  1225.       {
  1226.         unsigned int number_of_maus;
  1227.         unsigned int i;
  1228.         next_byte(&(ieee->h));
  1229.         number_of_maus = must_parse_int(&(ieee->h));
  1230.  
  1231.         for (i = 0; i < number_of_maus; i++) {
  1232.           location_ptr[current_map->pc++]= this_byte(&(ieee->h));
  1233.           next_byte(&(ieee->h));
  1234.         }
  1235.       }
  1236.     break;
  1237.  
  1238.       case ieee_load_with_relocation_enum:
  1239.       {
  1240.         boolean loop = true;
  1241.         next_byte(&(ieee->h));
  1242.         while (loop) 
  1243.         {
  1244.           switch (this_byte(&(ieee->h))) 
  1245.               {
  1246.               case ieee_variable_R_enum:
  1247.  
  1248.               case ieee_function_signed_open_b_enum:
  1249.               case ieee_function_unsigned_open_b_enum:
  1250.               case ieee_function_either_open_b_enum:
  1251.               {
  1252.                 unsigned int extra = 4;
  1253.                 boolean pcrel = false;
  1254.  
  1255.                 ieee_reloc_type *r = 
  1256.                   (ieee_reloc_type *) bfd_alloc(ieee->h.abfd,
  1257.                                 sizeof(ieee_reloc_type));
  1258.  
  1259.                 *(current_map->reloc_tail_ptr) = r;
  1260.                 current_map->reloc_tail_ptr= &r->next;
  1261.                 r->next = (ieee_reloc_type *)NULL;
  1262.                 next_byte(&(ieee->h));
  1263.                 parse_expression(ieee,
  1264.                          &r->relent.addend,
  1265.                          &r->relent.section,
  1266.                          &r->symbol,
  1267.                          &pcrel, &extra);
  1268.                 r->relent.address = current_map->pc;
  1269.                 s->reloc_count++;
  1270.  
  1271.                 if (this_byte(&(ieee->h)) == (int)ieee_comma) {
  1272.                   next_byte(&(ieee->h));
  1273.                   /* Fetch number of bytes to pad */
  1274.                   extra = must_parse_int(&(ieee->h));
  1275.                 };
  1276.            
  1277.                 switch (this_byte(&(ieee->h))) {
  1278.                 case ieee_function_signed_close_b_enum:
  1279.                   next_byte(&(ieee->h));
  1280.                   break;
  1281.                 case ieee_function_unsigned_close_b_enum:
  1282.                   next_byte(&(ieee->h));
  1283.                   break;
  1284.                 case ieee_function_either_close_b_enum:
  1285.                   next_byte(&(ieee->h));
  1286.                   break;
  1287.                 default:
  1288.                   break;
  1289.                 }
  1290.                 /* Build a relocation entry for this type */
  1291.                 /* If pc rel then stick -ve pc into instruction
  1292.                    and take out of reloc ..
  1293.  
  1294.                    I've changed this. It's all too
  1295.                    complicated. I keep 0 in the
  1296.                    instruction  now.
  1297.                    */
  1298.             
  1299.                 switch (extra) 
  1300.                 {
  1301.                 case 0:
  1302.                 case 4:
  1303.                   
  1304.                   if (pcrel == true) 
  1305.                       {
  1306. #if KEEPMINUSPCININST
  1307.                     bfd_put_32(ieee->h.abfd, -current_map->pc, location_ptr +
  1308.                             current_map->pc);
  1309.                     r->relent.howto = &rel32_howto;
  1310.                     r->relent.addend -=
  1311.                       current_map->pc;
  1312. #else
  1313.                     bfd_put_32(ieee->h.abfd,0, location_ptr +
  1314.                             current_map->pc);
  1315.                     r->relent.howto = &rel32_howto;
  1316. #endif
  1317.                       }
  1318.                   else 
  1319.                       {
  1320.                     bfd_put_32(ieee->h.abfd, 0, location_ptr +
  1321.                             current_map->pc);
  1322.                     r->relent.howto = &abs32_howto;
  1323.                       }
  1324.                   current_map->pc +=4;
  1325.                   break;
  1326.                 case 2:
  1327.                   if (pcrel == true) {
  1328. #if KEEPMINUSPCININST
  1329.                     bfd_put_16(ieee->h.abfd, (int)(-current_map->pc),  location_ptr +current_map->pc);
  1330.                     r->relent.addend -= current_map->pc;
  1331.                     r->relent.howto = &rel16_howto;
  1332. #else
  1333.  
  1334.                     bfd_put_16(ieee->h.abfd, 0,  location_ptr +current_map->pc);
  1335.                     r->relent.howto = &rel16_howto;
  1336. #endif
  1337.                   }
  1338.  
  1339.                   else {
  1340.                     bfd_put_16(ieee->h.abfd, 0,  location_ptr +current_map->pc);
  1341.                     r->relent.howto = &abs16_howto;
  1342.                   }
  1343.                   current_map->pc +=2;
  1344.                   break;
  1345.                 case 1:
  1346.                   if (pcrel == true) {
  1347. #if KEEPMINUSPCININST
  1348.                     bfd_put_8(ieee->h.abfd, (int)(-current_map->pc),  location_ptr +current_map->pc);
  1349.                     r->relent.addend -= current_map->pc;
  1350.                     r->relent.howto = &rel8_howto;
  1351. #else
  1352.                     bfd_put_8(ieee->h.abfd,0,  location_ptr +current_map->pc);
  1353.                     r->relent.howto = &rel8_howto;
  1354. #endif
  1355.                   }
  1356.                   else {
  1357.                     bfd_put_8(ieee->h.abfd, 0,  location_ptr +current_map->pc);
  1358.                     r->relent.howto = &abs8_howto;
  1359.                   }
  1360.                   current_map->pc +=1;
  1361.                   break;
  1362.  
  1363.                 default:
  1364.                   BFD_FAIL();
  1365.                   break;
  1366.                 }
  1367.               }
  1368.             break;
  1369.               default: 
  1370.               {
  1371.                 bfd_vma this_size ;
  1372.                 if (parse_int(&(ieee->h), &this_size) == true) {
  1373.                   unsigned int i;
  1374.                   for (i = 0; i < this_size; i++) {
  1375.                 location_ptr[current_map->pc ++] = this_byte(&(ieee->h));
  1376.                 next_byte(&(ieee->h));
  1377.                   }
  1378.                 }
  1379.                 else {
  1380.                   loop = false;
  1381.                 }
  1382.               }
  1383.               }
  1384.         }
  1385.       }
  1386.       }
  1387. }
  1388.  
  1389. /* Read in all the section data and relocation stuff too */
  1390. static boolean 
  1391. DEFUN(ieee_slurp_section_data,(abfd),
  1392.       bfd *abfd)
  1393. {
  1394.   bfd_byte *location_ptr = (bfd_byte *)NULL;
  1395.   ieee_data_type *ieee = ieee_data(abfd);
  1396.   unsigned int section_number ;
  1397.  
  1398.   ieee_per_section_type *current_map = (ieee_per_section_type *)NULL;
  1399.   asection *s;
  1400.   /* Seek to the start of the data area */
  1401.   if (ieee->read_data== true)  return true;
  1402.   ieee->read_data = true;
  1403.   ieee_seek(abfd, ieee->w.r.data_part);
  1404.  
  1405.   /* Allocate enough space for all the section contents */
  1406.  
  1407.  
  1408.   for (s = abfd->sections; s != (asection *)NULL; s = s->next) {
  1409.     ieee_per_section_type *per = (ieee_per_section_type *) s->used_by_bfd;
  1410.     per->data = (bfd_byte *) bfd_alloc(ieee->h.abfd, s->size);
  1411.     /*SUPPRESS 68*/
  1412.     per->reloc_tail_ptr =
  1413.       (ieee_reloc_type **)&(s->relocation);
  1414.   }
  1415.  
  1416.  
  1417.  
  1418.   while (true) {
  1419.     switch (this_byte(&(ieee->h))) 
  1420.     {
  1421.       /* IF we see anything strange then quit */
  1422.     default:
  1423.       return true;
  1424.  
  1425.     case ieee_set_current_section_enum:
  1426.       next_byte(&(ieee->h));
  1427.       section_number = must_parse_int(&(ieee->h));
  1428.       s = ieee->section_table[section_number];
  1429.       current_map = (ieee_per_section_type *) s->used_by_bfd;
  1430.       location_ptr = current_map->data - s->vma;
  1431.       /* The document I have says that Microtec's compilers reset */
  1432.       /* this after a sec section, even though the standard says not */
  1433.       /* to. SO .. */
  1434.       current_map->pc =s->vma;
  1435.       break;
  1436.  
  1437.  
  1438.     case ieee_e2_first_byte_enum:
  1439.       next_byte(&(ieee->h));
  1440.       switch (this_byte(&(ieee->h)))
  1441.           {
  1442.           case ieee_set_current_pc_enum & 0xff:
  1443.           {
  1444.             bfd_vma value;
  1445.             asection *dsection;
  1446.             ieee_symbol_index_type symbol;
  1447.             unsigned int extra;
  1448.             boolean pcrel;
  1449.             next_byte(&(ieee->h));
  1450.             must_parse_int(&(ieee->h)); /* Thow away section #*/
  1451.             parse_expression(ieee, &value, &dsection, &symbol,
  1452.                      &pcrel, &extra);
  1453.             current_map->pc = value;
  1454.             BFD_ASSERT((unsigned)(value - s->vma) <= s->size);
  1455.           }
  1456.         break;
  1457.  
  1458.           case ieee_value_starting_address_enum & 0xff:
  1459.         /* We've got to the end of the data now - */
  1460.         return true;
  1461.           default:
  1462.         BFD_FAIL();
  1463.         return true;
  1464.           }
  1465.       break;
  1466.     case ieee_repeat_data_enum:
  1467.         {
  1468.           /* Repeat the following LD or LR n times - we do this by
  1469.          remembering the stream pointer before running it and
  1470.          resetting it and running it n times. We special case
  1471.          the repetition of a repeat_data/load_constant
  1472.          */
  1473.  
  1474.           unsigned int iterations ;
  1475.           uint8e_type *start ;
  1476.           next_byte(&(ieee->h));
  1477.           iterations = must_parse_int(&(ieee->h));
  1478.           start =  ieee->h.input_p;
  1479.           if (start[0] == (int)ieee_load_constant_bytes_enum &&
  1480.           start[1] == 1) {
  1481.         while (iterations != 0) {
  1482.           location_ptr[current_map->pc++] = start[2];
  1483.           iterations--;
  1484.         }
  1485.         next_byte(&(ieee->h));
  1486.         next_byte(&(ieee->h));
  1487.         next_byte(&(ieee->h));
  1488.           }
  1489.           else {
  1490.         while (iterations != 0) {
  1491.           ieee->h.input_p = start;
  1492.           do_one(ieee, current_map, location_ptr,s);
  1493.           iterations --;
  1494.         }
  1495.           }
  1496.         }
  1497.       break;
  1498.     case ieee_load_constant_bytes_enum:
  1499.     case ieee_load_with_relocation_enum:
  1500.         {
  1501.           do_one(ieee, current_map, location_ptr,s);
  1502.         }
  1503.     }
  1504.   }
  1505. }
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511. boolean
  1512. DEFUN(ieee_new_section_hook,(abfd, newsect),
  1513.       bfd *abfd AND
  1514.       asection *newsect)
  1515. {
  1516.   newsect->used_by_bfd = (PTR)
  1517.     bfd_alloc(abfd, sizeof(ieee_per_section_type));
  1518.   ieee_per_section( newsect)->data = (bfd_byte *)NULL;
  1519.   ieee_per_section(newsect)->section = newsect;
  1520.   return true;
  1521. }
  1522.  
  1523.  
  1524. unsigned int
  1525. DEFUN(ieee_get_reloc_upper_bound,(abfd, asect),
  1526.       bfd *abfd AND
  1527.       sec_ptr asect)
  1528. {
  1529.   ieee_slurp_section_data(abfd);
  1530.   return (asect->reloc_count+1) * sizeof(arelent *);
  1531. }
  1532.  
  1533. static boolean
  1534. DEFUN(ieee_get_section_contents,(abfd, section, location, offset, count),
  1535.       bfd *abfd AND
  1536.       sec_ptr section AND
  1537.       PTR location AND
  1538.       file_ptr offset AND
  1539.       bfd_size_type count)
  1540. {
  1541.   ieee_per_section_type *p = (ieee_per_section_type *) section->used_by_bfd;
  1542.   ieee_slurp_section_data(abfd);
  1543.   (void)  memcpy((PTR)location, (PTR)(p->data + offset), (unsigned)count);
  1544.   return true;
  1545. }
  1546.  
  1547.  
  1548. unsigned int
  1549. DEFUN(ieee_canonicalize_reloc,(abfd, section, relptr, symbols),
  1550.       bfd *abfd AND
  1551.       sec_ptr section AND
  1552.       arelent **relptr AND
  1553.       asymbol **symbols)
  1554. {
  1555. /*  ieee_per_section_type *p = (ieee_per_section_type *) section->used_by_bfd;*/
  1556.   ieee_reloc_type *src = (ieee_reloc_type *)(section->relocation);
  1557.   ieee_data_type *ieee = ieee_data(abfd);
  1558.  
  1559.   while (src != (ieee_reloc_type *)NULL) {
  1560.     /* Work out which symbol to attach it this reloc to */
  1561.     switch (src->symbol.letter) {
  1562.     case 'X':
  1563.       src->relent.sym_ptr_ptr =
  1564.     symbols + src->symbol.index +  ieee->external_reference_base_offset;
  1565.       break;
  1566.     case 0:
  1567.       src->relent.sym_ptr_ptr = (asymbol **)NULL;
  1568.       break;
  1569.     default:
  1570.  
  1571.       BFD_FAIL();
  1572.     }
  1573.     *relptr++ = &src->relent;
  1574.     src = src->next;
  1575.   }
  1576.   *relptr = (arelent *)NULL;
  1577.   return section->reloc_count;
  1578. }
  1579.  
  1580.  
  1581.  
  1582. static int 
  1583. DEFUN(comp,(ap, bp),
  1584.      CONST PTR ap AND
  1585.      CONST PTR bp)
  1586. {
  1587.   arelent *a = *((arelent **)ap);
  1588.   arelent *b = *((arelent **)bp);
  1589.   return a->address - b->address;
  1590. }
  1591.  
  1592. /*
  1593. Write the section headers
  1594. */
  1595.  
  1596. static void
  1597. DEFUN(ieee_write_section_part,(abfd),
  1598.       bfd *abfd)
  1599. {
  1600.   ieee_data_type *ieee = ieee_data(abfd);
  1601.   asection *s;
  1602.   ieee->w.r.section_part = bfd_tell(abfd);
  1603.   for (s = abfd->sections; s != (asection *)NULL; s=s->next) {
  1604.     ieee_write_byte(abfd, ieee_section_type_enum);
  1605.     ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
  1606.  
  1607.     if (abfd->flags & EXEC_P) 
  1608.     {
  1609.       /* This image is executable, so output absolute sections */
  1610.       ieee_write_byte(abfd, ieee_variable_A_enum);
  1611.       ieee_write_byte(abfd, ieee_variable_S_enum);
  1612.     }
  1613.     else  
  1614.     {
  1615.       ieee_write_byte(abfd, ieee_variable_C_enum);
  1616.     }
  1617.  
  1618.     switch (s->flags &(SEC_CODE | SEC_DATA | SEC_ROM)) 
  1619.     {
  1620.     case SEC_CODE | SEC_LOAD:
  1621.     case SEC_CODE:
  1622.       ieee_write_byte(abfd, ieee_variable_P_enum);
  1623.       break;
  1624.     case SEC_DATA:
  1625.     default:
  1626.       ieee_write_byte(abfd, ieee_variable_D_enum);
  1627.       break;
  1628.     case SEC_ROM:
  1629.     case SEC_ROM | SEC_DATA:
  1630.     case SEC_ROM | SEC_LOAD:
  1631.     case SEC_ROM | SEC_DATA | SEC_LOAD:
  1632.  
  1633.       ieee_write_byte(abfd, ieee_variable_R_enum);
  1634.     }
  1635.  
  1636.  
  1637.     ieee_write_id(abfd, s->name);
  1638. #if 0
  1639.     ieee_write_int(abfd, 0);    /* Parent */
  1640.     ieee_write_int(abfd, 0);    /* Brother */
  1641.     ieee_write_int(abfd, 0);    /* Context */
  1642. #endif
  1643.     /* Alignment */
  1644.     ieee_write_byte(abfd, ieee_section_alignment_enum);
  1645.     ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
  1646.     ieee_write_int(abfd, 1 << s->alignment_power);
  1647.  
  1648.     /* Size */
  1649.     ieee_write_2bytes(abfd, ieee_section_size_enum);
  1650.     ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
  1651.     ieee_write_int(abfd, s->size);
  1652.     if (abfd->flags & EXEC_P) {
  1653.       /* Relocateable sections don't have asl records */
  1654.       /* Vma */
  1655.       ieee_write_2bytes(abfd, ieee_section_base_address_enum);
  1656.       ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
  1657.       ieee_write_int(abfd, s->vma);
  1658.     }
  1659.  
  1660.   }
  1661. }
  1662.  
  1663.  
  1664.  
  1665. static void 
  1666. DEFUN(do_with_relocs,(abfd, s),
  1667.       bfd *abfd AND
  1668.       asection *s)
  1669. {
  1670.   unsigned int relocs_to_go = s->reloc_count;
  1671.  
  1672.  
  1673.   bfd_byte *stream = ieee_per_section(s)->data;
  1674.   arelent **p = s->orelocation;
  1675.  
  1676.   bfd_size_type current_byte_index = 0;
  1677.  
  1678.   qsort(s->orelocation,
  1679.     relocs_to_go,
  1680.     sizeof(arelent **),
  1681.     comp);
  1682.  
  1683.   /* Output the section preheader */
  1684.   ieee_write_byte(abfd, ieee_set_current_section_enum);
  1685.   ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
  1686.  
  1687.   ieee_write_twobyte(abfd, ieee_set_current_pc_enum);
  1688.   ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
  1689.   ieee_write_expression(abfd, 0, s, 0, 0, 0);
  1690.  
  1691.   if (relocs_to_go == 0) 
  1692.       {
  1693.     /* If there arn't any relocations then output the load constant byte
  1694.        opcode rather than the load with relocation opcode */
  1695.  
  1696.     while (current_byte_index < s->size) {
  1697.       bfd_size_type run;
  1698.       unsigned int MAXRUN  = 32;
  1699.       run = MAXRUN;
  1700.       if (run > s->size - current_byte_index) {
  1701.         run = s->size - current_byte_index;
  1702.       }
  1703.  
  1704.       if (run != 0) {
  1705.         ieee_write_byte(abfd, ieee_load_constant_bytes_enum);
  1706.         /* Output a stream of bytes */
  1707.         ieee_write_int(abfd, run);
  1708.         bfd_write((PTR)(stream + current_byte_index), 
  1709.               1,
  1710.               run,
  1711.               abfd);
  1712.         current_byte_index += run;
  1713.       }
  1714.     }
  1715.       }
  1716.   else 
  1717.       {
  1718.     ieee_write_byte(abfd, ieee_load_with_relocation_enum);
  1719.  
  1720.  
  1721.     /* Output the data stream as the longest sequence of bytes
  1722.        possible, allowing for the a reasonable packet size and
  1723.        relocation stuffs */
  1724.  
  1725.     if ((PTR)stream == (PTR)NULL) {
  1726.       /* Outputting a section without data, fill it up */
  1727.       stream = (uint8e_type *)(bfd_alloc(abfd, s->size));
  1728.       memset((PTR)stream, 0, s->size);
  1729.     }
  1730.     while (current_byte_index < s->size) {
  1731.       bfd_size_type run;
  1732.       unsigned int MAXRUN = 32;
  1733.       if (relocs_to_go) {
  1734.         run = (*p)->address - current_byte_index;
  1735.       }
  1736.       else {
  1737.         run = MAXRUN;
  1738.       }
  1739.       if (run > s->size - current_byte_index) {
  1740.         run = s->size - current_byte_index;
  1741.       }
  1742.  
  1743.       if (run != 0) {
  1744.         /* Output a stream of bytes */
  1745.         ieee_write_int(abfd, run);
  1746.         bfd_write((PTR)(stream + current_byte_index), 
  1747.               1,
  1748.               run,
  1749.               abfd);
  1750.         current_byte_index += run;
  1751.       }
  1752.       /* Output any relocations here */
  1753.       if (relocs_to_go && (*p) && (*p)->address == current_byte_index) {
  1754.         while (relocs_to_go && (*p) && (*p)->address == current_byte_index) {
  1755.  
  1756.           arelent *r = *p;
  1757.           bfd_vma ov;
  1758.  
  1759. #if 0
  1760.           if (r->howto->pc_relative) {
  1761.         r->addend += current_byte_index ;
  1762.           }
  1763. #endif
  1764.  
  1765.           switch (r->howto->size) {
  1766.           case 2:
  1767.  
  1768.         ov = bfd_get_32(abfd,
  1769.                 stream+current_byte_index);
  1770.         current_byte_index +=4;
  1771.         break;
  1772.           case 1:
  1773.         ov = bfd_get_16(abfd,
  1774.                 stream+current_byte_index);
  1775.         current_byte_index +=2;
  1776.         break;
  1777.           case 0:
  1778.         ov = bfd_get_8(abfd,
  1779.                    stream+current_byte_index);
  1780.         current_byte_index ++;
  1781.         break;
  1782.           default:
  1783.         ov = 0;
  1784.         BFD_FAIL();
  1785.           }
  1786.           ieee_write_byte(abfd, ieee_function_either_open_b_enum);
  1787.  
  1788.           if (r->sym_ptr_ptr != (asymbol **)NULL) {
  1789.         ieee_write_expression(abfd, r->addend + ov,
  1790.                       r->section,
  1791.                       *(r->sym_ptr_ptr),
  1792.                       r->howto->pc_relative, s->index);
  1793.           }
  1794.           else {
  1795.         ieee_write_expression(abfd, r->addend + ov,
  1796.                       r->section,
  1797.                       (asymbol *)NULL,
  1798.                       r->howto->pc_relative, s->index);
  1799.           }
  1800.  
  1801.           if (1 || r->howto->size != 2) {
  1802.         ieee_write_byte(abfd, ieee_comma);
  1803.         ieee_write_int(abfd, 1<< r->howto->size);
  1804.           }
  1805.           ieee_write_byte(abfd,
  1806.                   ieee_function_either_close_b_enum);
  1807.  
  1808.           relocs_to_go --;
  1809.           p++;
  1810.         }
  1811.  
  1812.       }
  1813.     }
  1814.       }
  1815. }
  1816.  
  1817. /* If there are no relocations in the output section then we can
  1818. be clever about how we write. We block items up into a max of 127
  1819. bytes */
  1820.  
  1821. static void 
  1822. DEFUN(do_as_repeat, (abfd, s),
  1823.       bfd *abfd AND
  1824.       asection *s)
  1825. {
  1826.   ieee_write_byte(abfd, ieee_set_current_section_enum);
  1827.   ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
  1828.   ieee_write_byte(abfd, ieee_set_current_pc_enum >> 8);
  1829.  ieee_write_byte(abfd, ieee_set_current_pc_enum  & 0xff);
  1830.   ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
  1831.   ieee_write_int(abfd,  s->vma );
  1832.  
  1833.   ieee_write_byte(abfd,ieee_repeat_data_enum);
  1834.   ieee_write_int(abfd, s->size);
  1835.   ieee_write_byte(abfd, ieee_load_constant_bytes_enum);
  1836.   ieee_write_byte(abfd, 1);
  1837.   ieee_write_byte(abfd, 0);
  1838. }
  1839.  
  1840. static void 
  1841. DEFUN(do_without_relocs, (abfd, s),
  1842.       bfd *abfd AND
  1843.       asection *s)
  1844. {
  1845.   bfd_byte *stream = ieee_per_section(s)->data;
  1846.  
  1847.   if (stream == 0 || ((s->flags & SEC_LOAD) == 0)) 
  1848.       {
  1849.     do_as_repeat(abfd, s);
  1850.       }
  1851.   else 
  1852.       {
  1853.     unsigned int i;
  1854.     for (i = 0; i < s->size; i++) {
  1855.       if (stream[i] != 0) {
  1856.         do_with_relocs(abfd, s);
  1857.         return;
  1858.       }
  1859.     }
  1860.     do_as_repeat(abfd, s);
  1861.       }
  1862.   
  1863. }
  1864.  
  1865.  
  1866. static unsigned char *output_ptr_start;
  1867. static unsigned char *output_ptr;
  1868. static unsigned char *output_ptr_end;
  1869. static unsigned char *input_ptr_start;
  1870. static unsigned char *input_ptr;
  1871. static unsigned char *input_ptr_end;
  1872. static bfd *input_bfd;
  1873. static bfd *output_bfd;
  1874. static int output_buffer;
  1875.  
  1876. static void fill()
  1877. {
  1878.   bfd_read((PTR)input_ptr_start, 1, input_ptr_end - input_ptr_start, input_bfd);
  1879.   input_ptr = input_ptr_start;
  1880. }
  1881. static void flush()
  1882. {
  1883.   bfd_write((PTR)(output_ptr_start),1,output_ptr - output_ptr_start, output_bfd);
  1884.   output_ptr = output_ptr_start;
  1885.   output_buffer++;
  1886. }
  1887.  
  1888. #define THIS() ( *input_ptr )
  1889. #define NEXT() { input_ptr++; if (input_ptr == input_ptr_end) fill(); }
  1890. #define OUT(x) { *output_ptr++ = (x); if(output_ptr == output_ptr_end)  flush(); }
  1891.  
  1892. static void write_int(value)
  1893. int value;
  1894. {
  1895.   if (value >= 0 && value <= 127) {
  1896.     OUT(value);
  1897.   }
  1898.   else {
  1899.     unsigned int length;
  1900.     /* How many significant bytes ? */
  1901.     /* FIXME FOR LONGER INTS */
  1902.     if (value & 0xff000000) {
  1903.       length = 4;
  1904.     }
  1905.     else if (value & 0x00ff0000) {
  1906.       length  = 3;
  1907.     }
  1908.     else if (value & 0x0000ff00) {
  1909.       length = 2;
  1910.     }
  1911.     else length = 1;
  1912.  
  1913.     OUT((int)ieee_number_repeat_start_enum + length);
  1914.     switch (length) {
  1915.     case 4:
  1916.       OUT( value >> 24);
  1917.     case 3:
  1918.       OUT( value >> 16);
  1919.     case 2:
  1920.       OUT( value >> 8);
  1921.     case 1:
  1922.       OUT( value);
  1923.     }
  1924.  
  1925.   }
  1926. }
  1927. static void copy_id() 
  1928. {
  1929.   int length  = THIS();
  1930.   char ch;
  1931.   OUT(length);
  1932.   NEXT();
  1933.   while (length--) {
  1934.     ch = THIS();
  1935.     OUT(ch);
  1936.     NEXT();
  1937.   }
  1938. }
  1939. #define VAR(x) ((x | 0x80))
  1940. static void copy_expression()
  1941. {
  1942.   int stack[10];
  1943.   int *tos = stack;
  1944.   int value = 0;
  1945.   while (1) {
  1946.     switch (THIS()) {
  1947.     case 0x84:
  1948.       NEXT();
  1949.       value =  THIS(); NEXT();
  1950.       value = (value << 8) | THIS(); NEXT();
  1951.       value = (value << 8) | THIS(); NEXT();
  1952.       value = (value << 8) | THIS(); NEXT();
  1953.       *tos ++ = value;
  1954.       break;
  1955.     case 0x83:
  1956.       NEXT();
  1957.       value =  THIS(); NEXT();
  1958.       value = (value << 8) | THIS(); NEXT();
  1959.       value = (value << 8) | THIS(); NEXT();
  1960.       *tos ++ = value;
  1961.       break;
  1962.     case 0x82:    
  1963.       NEXT();
  1964.       value =  THIS(); NEXT();
  1965.       value = (value << 8) | THIS(); NEXT();
  1966.       *tos ++ = value;
  1967.       break;
  1968.     case 0x81:
  1969.       NEXT();
  1970.       value =  THIS(); NEXT();
  1971.       *tos ++ = value;
  1972.       break;
  1973.       case 0x80:
  1974.       NEXT();
  1975.       *tos ++ = 0;
  1976.       break;
  1977.     default:
  1978.       if (THIS() >0x84) {
  1979.     /* Not a number, just bug out with the answer */
  1980.     write_int(*(--tos));
  1981.     return;
  1982.       }
  1983.       *tos++ = THIS();
  1984. NEXT();
  1985.       value = 0;
  1986.       break;
  1987.     case 0xa5:
  1988.       /* PLUS anything */
  1989.     {
  1990.       int value = *(--tos);
  1991.       value += *(--tos);
  1992.       *tos++ = value;
  1993.       NEXT();
  1994.     }
  1995.       break;
  1996.     case VAR('R') :
  1997.     {
  1998.       int section_number ;
  1999.       ieee_data_type *ieee;
  2000.       asection *s;
  2001.       NEXT();
  2002.       section_number = THIS();
  2003.     
  2004.       NEXT();
  2005.       ieee= ieee_data(input_bfd);
  2006.       s =     ieee->section_table[section_number];
  2007.       if (s->output_section) {
  2008.         value = s->output_section->vma ;
  2009.       } else { value = 0; }
  2010.       value += s->output_offset;
  2011.       *tos++ = value;
  2012.       value = 0;
  2013.     }
  2014.       break;
  2015.     case 0x90:
  2016.     {    
  2017.       NEXT();
  2018.       write_int(*(--tos));
  2019.       OUT(0x90);
  2020.       return;
  2021.  
  2022.     }
  2023.     }
  2024.   }
  2025.  
  2026. }
  2027.  
  2028. /* Drop the int in the buffer, and copy a null into the gap, which we
  2029.    will overwrite later */
  2030.  
  2031. struct output_buffer_struct {
  2032. unsigned  char *ptrp;
  2033.   int buffer;
  2034. } ;
  2035.  
  2036. static void
  2037. DEFUN(fill_int,(buf),
  2038.       struct output_buffer_struct *buf)
  2039. {
  2040.   if (buf->buffer == output_buffer) {
  2041.     /* Still a chance to output the size */
  2042.     int value = output_ptr - buf->ptrp + 3;
  2043.     buf->ptrp[0] =  value >> 24;
  2044.     buf->ptrp[1] =  value >> 16;
  2045.     buf->ptrp[2] =  value >> 8;
  2046.     buf->ptrp[3] =  value >> 0;
  2047.   }
  2048.  
  2049. }
  2050. static void
  2051. DEFUN(drop_int,(buf),
  2052.       struct output_buffer_struct *buf)
  2053. {
  2054.   int type = THIS();
  2055.   int ch;
  2056.   if (type <= 0x84) {
  2057.     NEXT();
  2058.     switch(type) {
  2059.     case 0x84: ch = THIS(); NEXT();
  2060.     case 0x83: ch = THIS(); NEXT();
  2061.     case 0x82: ch = THIS(); NEXT();
  2062.     case 0x81: ch = THIS(); NEXT();
  2063.     case 0x80: break;
  2064.     }
  2065.   }
  2066.   OUT(0x84);
  2067.   buf->ptrp = output_ptr;
  2068.   buf->buffer  = output_buffer;
  2069.   OUT(0);OUT(0);OUT(0);OUT(0);
  2070. }
  2071.  
  2072. static void copy_int()
  2073. {
  2074.   int type = THIS();
  2075.   int ch;
  2076.   if (type <= 0x84) {
  2077.     OUT(type);
  2078.     NEXT();
  2079.     switch(type) {
  2080.     case 0x84: ch = THIS(); NEXT(); OUT(ch);
  2081.     case 0x83: ch = THIS(); NEXT(); OUT(ch);
  2082.     case 0x82: ch = THIS(); NEXT(); OUT(ch);
  2083.     case 0x81: ch = THIS(); NEXT(); OUT(ch);
  2084.     case 0x80: break;
  2085.     }
  2086.   }
  2087. }
  2088.  
  2089. #define ID copy_id()
  2090. #define INT copy_int()
  2091. #define EXP copy_expression()
  2092. static void copy_till_end();
  2093. #define INTn(q) copy_int()
  2094. #define EXPn(q) copy_expression()
  2095. static void f1_record()
  2096. {
  2097.   int ch;
  2098.   /* ATN record */
  2099.   NEXT();
  2100.   ch = THIS();
  2101.   switch (ch)
  2102.       {
  2103.       default:
  2104.     OUT(0xf1); OUT(ch);
  2105.     break;
  2106.       case 0xc9:
  2107.     NEXT();
  2108.     OUT(0xf1); OUT(0xc9);
  2109.     INT; INT; ch = THIS(); 
  2110.     switch (ch) 
  2111.         {
  2112.         case 0x16: NEXT();break;
  2113.         case 0x01: NEXT();break;
  2114.         case 0x00: NEXT(); INT; break;
  2115.         case 0x03: NEXT(); INT; break;
  2116.         case 0x13: EXPn(instruction address); break;
  2117.         default:
  2118.           break;
  2119.         }
  2120.     break;
  2121.       case 0xd8:
  2122.     /* EXternal ref */
  2123.     NEXT();    
  2124.     OUT(0xf1); OUT(0xd8);
  2125.     EXP ; EXP; EXP; EXP;
  2126.     break;
  2127.       case 0xce:
  2128.     NEXT();
  2129.     OUT(0xf1);OUT(0xce); INT; INT; ch = THIS(); INT;
  2130.     switch (ch) {
  2131.     case 0x01:
  2132.       INT; INT; break;
  2133.     case 0x02:
  2134.       INT; break;
  2135.     case 0x04:
  2136.       EXPn(external function); break;
  2137.     case 0x05:
  2138.       break;
  2139.     case 0x07: INTn(line number); INT;
  2140.     case 0x08: break;
  2141.     case 0x0a: INTn(locked register); INT; break;
  2142.     case 0x3f: copy_till_end(); break;
  2143.     case 0x3e: copy_till_end(); break;
  2144.     case 0x40: copy_till_end(); break;
  2145.     case 0x41: ID; break;
  2146.     }
  2147.       }
  2148.  
  2149. }
  2150. static void f0_record()
  2151. {
  2152.   /* Attribute record */
  2153.   NEXT();
  2154.   OUT(0xf0);
  2155.   INTn(Symbol name );
  2156.   ID;
  2157. }
  2158. static void copy_till_end()
  2159. {
  2160.   int  ch = THIS();
  2161.   while (1) {
  2162.     while (ch <= 0x80) 
  2163.     {
  2164.       OUT(ch);
  2165.       NEXT();
  2166.       ch = THIS();
  2167.     }
  2168.     switch (ch) {
  2169.     case 0x84:
  2170.       OUT(THIS());
  2171.       NEXT();
  2172.     case 0x83:
  2173.       OUT(THIS());
  2174.       NEXT();
  2175.     case 0x82:
  2176.       OUT(THIS());
  2177.       NEXT();
  2178.     case 0x81:
  2179.       OUT(THIS());
  2180.       NEXT();
  2181.       OUT(THIS());
  2182.       NEXT();
  2183.  
  2184.       ch = THIS();
  2185.       break;
  2186.     default:
  2187.       return;
  2188.     }
  2189.   }    
  2190.  
  2191. }
  2192.  
  2193. static void f2_record()
  2194. {
  2195.   NEXT();
  2196.   OUT(0xf2);
  2197.   INT ;
  2198.   NEXT();
  2199.   OUT(0xce);
  2200.   INT ;
  2201.   copy_till_end();
  2202. }
  2203.  
  2204.  
  2205. static void block();
  2206. static void f8_record()
  2207. {
  2208.   int ch;
  2209.   NEXT();
  2210.   ch = THIS();
  2211.   switch (ch) 
  2212.       {
  2213.       case 0x01:
  2214.       case 0x02:
  2215.       case 0x03:
  2216.     /* Unique typedefs for module */
  2217.     /* GLobal typedefs  */
  2218.     /* High level module scope beginning */
  2219.       {
  2220.         struct output_buffer_struct ob;
  2221.         NEXT();
  2222.         OUT(0xf8); OUT(ch);
  2223.         drop_int(&ob); ID ;
  2224.  
  2225.         block();
  2226.  
  2227.         NEXT();
  2228.         fill_int(&ob);
  2229.         OUT(0xf9);
  2230.       }
  2231.     break;
  2232.       case 0x04:    
  2233.     /* Global function */
  2234.       {
  2235.         struct output_buffer_struct ob;
  2236.         NEXT();
  2237.         OUT(0xf8); OUT(0x04);
  2238.         drop_int(&ob); ID ; INTn(stack size); INTn(ret val);
  2239.         EXPn(offset); 
  2240.  
  2241.         block();
  2242.  
  2243.         NEXT();
  2244.         OUT(0xf9);    
  2245.         EXPn(size of block);
  2246.         fill_int(&ob);
  2247.       }
  2248.     break;
  2249.  
  2250.       case 0x05:
  2251.     /* File name for source line numbers */
  2252.       {
  2253.         struct output_buffer_struct ob;
  2254.         NEXT();
  2255.         OUT(0xf8); OUT(0x05);
  2256.         drop_int(&ob);
  2257.         ID; INTn(year); INTn(month); INTn(day);
  2258.         INTn(hour); INTn(monute); INTn(second);
  2259.         block();
  2260.         NEXT();
  2261.         OUT(0xf9);
  2262.         fill_int(&ob);
  2263.       }
  2264.     break;
  2265.     
  2266.       case 0x06:
  2267.     /* Local function */
  2268.       { struct output_buffer_struct ob;
  2269.         NEXT(); OUT(0xf8); OUT(0x06);
  2270.         drop_int(&ob);
  2271.         ID; INTn(stack size); INTn(type return);
  2272.         EXPn(offset);
  2273.         block();
  2274.         NEXT();
  2275.         OUT(0xf9);
  2276.         EXPn(size);
  2277.         fill_int(&ob);
  2278.       }
  2279.     break;
  2280.     
  2281.       case 0x0a:
  2282.     /* Assembler module scope beginning -*/
  2283.       { struct output_buffer_struct ob;
  2284.  
  2285.         NEXT();
  2286.         OUT(0xf8); OUT(0x0a); 
  2287.         drop_int(&ob);
  2288.           ID; ID; INT; ID; INT; INT; INT; INT; INT; INT;
  2289.  
  2290.         block();    
  2291.  
  2292.         NEXT();
  2293.         OUT(0xf9);
  2294.         fill_int(&ob);
  2295.       }
  2296.     break;
  2297.       case 0x0b:
  2298.       {
  2299.         struct output_buffer_struct ob;
  2300.         NEXT();
  2301.         OUT(0xf8); OUT(0x0b); 
  2302.         drop_int(&ob); ID ; INT; INTn(section index); EXPn(offset); INTn(stuff);
  2303.  
  2304.         block();
  2305.  
  2306.         OUT(0xf9);
  2307.         NEXT();      
  2308.         EXPn(Size in Maus);
  2309.         fill_int(&ob);
  2310.       }
  2311.     break;
  2312.       }
  2313. }
  2314.  
  2315. static void e2_record()
  2316. {
  2317.   OUT(0xe2);
  2318.   NEXT();
  2319.   OUT(0xce);
  2320.   NEXT();
  2321.   INT;
  2322.   EXP;
  2323. }
  2324.  
  2325. static void DEFUN_VOID(block)
  2326. {
  2327.   int ch ;
  2328.   while (1) {
  2329.     ch = THIS();
  2330.     switch (ch) {
  2331.     case 0xe1:
  2332.     case 0xe5:
  2333.       return;
  2334.     case 0xf9:
  2335.       return;
  2336.     case 0xf0:
  2337.       f0_record();
  2338.       break;
  2339.     case 0xf1:
  2340.       f1_record();    
  2341.       break;
  2342.     case 0xf2:
  2343.       f2_record();
  2344.       break;
  2345.     case 0xf8:
  2346.       f8_record();
  2347.       break;
  2348.     case 0xe2:
  2349.       e2_record();
  2350.       break;
  2351.  
  2352.     }  
  2353.   }
  2354. }
  2355.   
  2356.  
  2357.  
  2358. /* relocate_debug, 
  2359.    moves all the debug information from the source bfd to the output
  2360.    bfd, and relocates any expressions it finds
  2361. */
  2362.  
  2363. static void
  2364. DEFUN(relocate_debug,(output, input),
  2365.       bfd *output AND
  2366.       bfd *input)
  2367. {
  2368. #define IBS 400
  2369. #define OBS 400
  2370.   unsigned char input_buffer[IBS];
  2371.  
  2372.   input_ptr_start = input_ptr = input_buffer;
  2373.   input_ptr_end = input_buffer + IBS;
  2374.   input_bfd = input;
  2375.   bfd_read((PTR)input_ptr_start, 1, IBS, input);
  2376.   block();
  2377. }
  2378. /* 
  2379.   During linking, we we told about the bfds which made up our
  2380.   contents, we have a list of them. They will still be open, so go to
  2381.   the debug info in each, and copy it out, relocating it as we go.
  2382. */
  2383.  
  2384. static void 
  2385. DEFUN(ieee_write_debug_part, (abfd),
  2386.       bfd *abfd)
  2387. {
  2388.   ieee_data_type *ieee = ieee_data(abfd);
  2389.   bfd_chain_type *chain = ieee->chain_root;
  2390.   unsigned char output_buffer[OBS];
  2391.   boolean some_debug = false;
  2392.   file_ptr here = bfd_tell(abfd);
  2393.  
  2394.   output_ptr_start = output_ptr = output_buffer ;
  2395.   output_ptr_end = output_buffer + OBS;
  2396.   output_ptr = output_buffer;
  2397.   output_bfd = abfd;
  2398.  
  2399.   if (chain == (bfd_chain_type *)NULL) {
  2400.     /* There is no debug info, so we'll fake some up */
  2401.     CONST static char fake[] = {
  2402.       0xf8, 0xa, 0, 5, 't', 't', 't', 't', 't', 0, 2, 3,
  2403.       '1','.','1',0x82, 1991>>8, 1991 & 0xff, 9, 20, 11, 07,50 };
  2404.     ieee->w.r.debug_information_part = 0;
  2405.  
  2406. #if 0
  2407. here;
  2408.  
  2409.  
  2410. /*    bfd_write(fake, 1, sizeof(fake), abfd);*/
  2411.     /* Now write a header for each section */
  2412.       {
  2413.     int i = 0;
  2414.     asection *s = abfd->sections;
  2415.     while (s) {
  2416.       ieee_write_byte(abfd, 0xf8);    
  2417.       ieee_write_byte(abfd, 0x0b);
  2418.       ieee_write_byte(abfd, 0);
  2419.       ieee_write_byte(abfd, 0);
  2420.       ieee_write_byte(abfd, 1);
  2421.       ieee_write_byte(abfd, i + IEEE_SECTION_NUMBER_BASE);
  2422.       ieee_write_expression(abfd, 0, s, 0, 0, 0);
  2423.       ieee_write_byte(abfd,0);
  2424.       ieee_write_byte(abfd, 0xf9);
  2425.       ieee_write_expression(abfd, s->size, 0, 0, 0, 0);    
  2426.       s = s->next;
  2427.       i++;
  2428.     }    
  2429.     /* Close the scope */
  2430.     ieee_write_byte(abfd, 0xf9);
  2431.       }
  2432. #endif
  2433.   }
  2434.   else{
  2435.     while (chain != (bfd_chain_type *)NULL) {
  2436.       bfd *entry = chain->this;
  2437.       ieee_data_type *entry_ieee = ieee_data(entry);
  2438.       if (entry_ieee->w.r.debug_information_part) {
  2439.     bfd_seek(entry, entry_ieee->w.r.debug_information_part, SEEK_SET);
  2440.     relocate_debug(abfd, entry);
  2441.       }
  2442.  
  2443.       chain = chain->next;
  2444.     }
  2445.     if (some_debug) {
  2446.       ieee->w.r.debug_information_part = here;
  2447.     }
  2448.     else {
  2449.       ieee->w.r.debug_information_part = 0;
  2450.     }
  2451.   }
  2452.   flush();
  2453.  
  2454. }  
  2455. /* write the data in an ieee way */
  2456. static void
  2457. DEFUN(ieee_write_data_part,(abfd),
  2458.       bfd *abfd)
  2459. {
  2460.   asection *s;
  2461.   ieee_data_type *ieee = ieee_data(abfd);
  2462.   ieee->w.r.data_part = bfd_tell(abfd);
  2463.   for (s = abfd->sections; s != (asection *)NULL; s = s->next) 
  2464.       {
  2465.     /* Sort the reloc records so we can insert them in the correct
  2466.        places */
  2467.     if (s->reloc_count != 0) 
  2468.         {
  2469.          do_with_relocs(abfd, s);
  2470.         }
  2471.     else
  2472.         {
  2473.           do_without_relocs(abfd, s);
  2474.         }
  2475.       }
  2476. }
  2477.  
  2478.  
  2479.  
  2480. static void
  2481. DEFUN(init_for_output,(abfd),
  2482.       bfd *abfd)
  2483. {
  2484.   asection *s; 
  2485.   for (s = abfd->sections; s != (asection *)NULL; s = s->next) {
  2486.     if (s->size != 0) {
  2487.       ieee_per_section(s)->data = (bfd_byte *)(bfd_alloc(abfd, s->size));
  2488.     }
  2489.   }
  2490. }
  2491.  
  2492. /** exec and core file sections */
  2493.  
  2494. /* set section contents is complicated with IEEE since the format is 
  2495. * not a byte image, but a record stream.
  2496. */
  2497. boolean
  2498. DEFUN(ieee_set_section_contents,(abfd, section, location, offset, count),
  2499.       bfd *abfd AND
  2500.       sec_ptr section AND
  2501.       PTR location AND
  2502.       file_ptr offset AND
  2503.       bfd_size_type count)
  2504. {
  2505.   if (ieee_per_section(section)->data == (bfd_byte *)NULL) {
  2506.     init_for_output(abfd);
  2507.   }
  2508.   (void) memcpy((PTR)(ieee_per_section(section)->data + offset),
  2509.         (PTR)location,
  2510.         (unsigned int)count);
  2511.   return true;
  2512. }
  2513.  
  2514. /*
  2515. write the external symbols of a file, IEEE considers two sorts of
  2516. external symbols, public, and referenced. It uses to internal forms
  2517. to index them as well. When we write them out we turn their symbol
  2518. values into indexes from the right base.
  2519. */
  2520. static void
  2521. DEFUN(ieee_write_external_part,(abfd),
  2522.       bfd *abfd)
  2523. {
  2524.   asymbol **q;
  2525.   ieee_data_type *ieee = ieee_data(abfd);
  2526.  
  2527.   unsigned int reference_index = IEEE_REFERENCE_BASE;
  2528.   unsigned int public_index = IEEE_PUBLIC_BASE+2;
  2529.   file_ptr here = bfd_tell(abfd);
  2530.   boolean hadone = false;
  2531.   if (abfd->outsymbols != (asymbol **)NULL) {
  2532.  
  2533.     for (q = abfd->outsymbols; *q  != (asymbol *)NULL; q++) {
  2534.       asymbol *p = *q;
  2535.       hadone = true;
  2536.       if (p->flags & BSF_UNDEFINED) {
  2537.     /* This must be a symbol reference .. */
  2538.     ieee_write_byte(abfd, ieee_external_reference_enum);
  2539.     ieee_write_int(abfd, reference_index);
  2540.     ieee_write_id(abfd, p->name);
  2541.     p->value = reference_index;
  2542.     reference_index++;
  2543.       }
  2544.       else if(p->flags & BSF_FORT_COMM) {
  2545.     /* This is a weak reference */
  2546.     ieee_write_byte(abfd, ieee_external_reference_enum);
  2547.     ieee_write_int(abfd, reference_index);
  2548.     ieee_write_id(abfd, p->name);
  2549.     ieee_write_byte(abfd, ieee_weak_external_reference_enum);
  2550.     ieee_write_int(abfd, reference_index);
  2551.     ieee_write_int(abfd, p->value);
  2552.     ieee_write_int(abfd, BFD_FORT_COMM_DEFAULT_VALUE);
  2553.     p->value = reference_index;
  2554.     reference_index++;
  2555.       }
  2556.       else if(p->flags & BSF_GLOBAL) {
  2557.     /* This must be a symbol definition */
  2558.  
  2559.  
  2560.     ieee_write_byte(abfd, ieee_external_symbol_enum);
  2561.     ieee_write_int(abfd, public_index );
  2562.     ieee_write_id(abfd, p->name);
  2563.  
  2564.     ieee_write_twobyte(abfd, ieee_attribute_record_enum);
  2565.     ieee_write_int(abfd, public_index );
  2566.     ieee_write_byte(abfd, 15);     /* instruction address */
  2567.     ieee_write_byte(abfd, 19);    /* static symbol */
  2568.     ieee_write_byte(abfd, 1);    /* one of them */
  2569.  
  2570.  
  2571.     /* Write out the value */
  2572.     ieee_write_2bytes(abfd, ieee_value_record_enum);
  2573.     ieee_write_int(abfd, public_index);
  2574.     if (p->section != (asection *)NULL)
  2575.         {
  2576.           if (abfd->flags & EXEC_P) 
  2577.           {
  2578.             /* If fully linked, then output all symbols
  2579.                relocated */
  2580.             ieee_write_int(abfd,
  2581.                    p->value + p->section->output_offset+ p->section->output_section->vma);
  2582.  
  2583.           }
  2584.           else { 
  2585.         ieee_write_expression(abfd,
  2586.                       p->value + p->section->output_offset,
  2587.                       p->section->output_section,
  2588.                       (asymbol *)NULL, false, 0);
  2589.           }
  2590.         }
  2591.     else
  2592.         {
  2593.           ieee_write_expression(abfd,
  2594.                     p->value,
  2595.                     (asection *)NULL,
  2596.                     (asymbol *)NULL, false, 0);
  2597.         }
  2598.     p->value = public_index;
  2599.     public_index++;
  2600.       }
  2601.       else {
  2602.     /* This can happen - when there are gaps in the symbols read */
  2603.     /* from an input ieee file */
  2604.       }
  2605.     }
  2606.   }
  2607.   if (hadone)
  2608.     ieee->w.r.external_part = here;
  2609.  
  2610. }
  2611.  
  2612.  
  2613. CONST static char exten[] = 
  2614.   {
  2615.     0xf0, 0x20, 0x00,                    
  2616.     0xf1, 0xce, 0x20, 0x00, 37, 3, 3,    /* Set version 3 rev 3       */
  2617.     0xf1, 0xce, 0x20, 0x00, 39, 2,    /* keep symbol in  original case */
  2618.     0xf1, 0xce, 0x20, 0x00, 38        /* set object type relocateable to x */
  2619.   };
  2620.  
  2621. CONST static char environ[] =
  2622.   {
  2623.     0xf0, 0x21, 0x00,
  2624.  
  2625. /*    0xf1, 0xce, 0x21, 00, 50, 0x82, 0x07, 0xc7, 0x09, 0x11, 0x11,
  2626.     0x19, 0x2c, 
  2627. */
  2628.     0xf1, 0xce, 0x21, 00, 52, 0x00, /* exec ok */
  2629.  
  2630.     0xf1, 0xce, 0x21, 0, 53, 0x03,    /* host unix */
  2631. /*    0xf1, 0xce, 0x21, 0, 54, 2,1,1    tool & version # */
  2632.   };
  2633.  
  2634. static
  2635. void 
  2636. DEFUN(ieee_write_me_part,(abfd),
  2637.       bfd *abfd)
  2638. {
  2639.   ieee_data_type *ieee= ieee_data(abfd);
  2640.   ieee->w.r.trailer_part = bfd_tell(abfd);
  2641.   if (abfd->start_address) {
  2642.     ieee->w.r.me_record = bfd_tell(abfd);
  2643.     ieee_write_2bytes(abfd, ieee_value_starting_address_enum);
  2644.     ieee_write_byte(abfd, ieee_function_either_open_b_enum);
  2645.     ieee_write_int(abfd, abfd->start_address);
  2646.     ieee_write_byte(abfd, ieee_function_either_close_b_enum);
  2647.   }
  2648.   else {
  2649.     ieee->w.r.me_record = bfd_tell(abfd);
  2650.   }
  2651.   ieee_write_byte(abfd, ieee_module_end_enum);
  2652.  
  2653. }
  2654. boolean
  2655. DEFUN(ieee_write_object_contents,(abfd),
  2656.       bfd *abfd)
  2657. {
  2658.   ieee_data_type *ieee = ieee_data(abfd);
  2659.   unsigned int i;
  2660.   file_ptr   old;
  2661.   /* Fast forward over the header area */
  2662.   bfd_seek(abfd, 0, 0);
  2663.   ieee_write_byte(abfd, ieee_module_beginning_enum);
  2664.  
  2665.   ieee_write_id(abfd, bfd_printable_name(abfd));
  2666.   ieee_write_id(abfd, abfd->filename);
  2667.  
  2668.  
  2669.  
  2670.  
  2671.   /* Fast forward over the variable bits */    
  2672.  
  2673.  
  2674.  
  2675.   ieee_write_byte(abfd, ieee_address_descriptor_enum);
  2676.  
  2677.   /* Bits per MAU */
  2678.   ieee_write_byte(abfd, bfd_arch_bits_per_byte(abfd));
  2679.   /* MAU's per address */
  2680.   ieee_write_byte(abfd, bfd_arch_bits_per_address(abfd)  /
  2681.           bfd_arch_bits_per_byte(abfd));
  2682.  
  2683.  
  2684.   old = bfd_tell(abfd);
  2685.   bfd_seek(abfd, 8 * N_W_VARIABLES, 1);
  2686.  
  2687.  
  2688.   ieee->w.r.extension_record = bfd_tell(abfd);
  2689.   bfd_write(exten, 1, sizeof(exten), abfd);
  2690.   if (abfd->flags & EXEC_P) 
  2691.     ieee_write_byte(abfd, 0x1); /* Absolute */
  2692.   else 
  2693.     ieee_write_byte(abfd, 0x2); /* Relocateable */    
  2694.   
  2695.   ieee->w.r.environmental_record = bfd_tell(abfd);
  2696.   bfd_write(environ, 1, sizeof(environ), abfd);
  2697.   output_bfd = abfd;
  2698.   flush();
  2699.  
  2700.   ieee_write_section_part(abfd);
  2701.   /*
  2702.     First write the symbols, this changes their values into table 
  2703.     indeces so we cant use it after this point
  2704.     */
  2705.   ieee_write_external_part(abfd);     
  2706.   /*  ieee_write_byte(abfd, ieee_record_seperator_enum);*/
  2707.  
  2708.  
  2709.   /*  ieee_write_byte(abfd, ieee_record_seperator_enum);*/
  2710.  
  2711.  
  2712.   /*
  2713.     Write any debugs we have been told about 
  2714.     */
  2715.   ieee_write_debug_part(abfd);
  2716.  
  2717.   /* 
  2718.     Can only write the data once the symbols have been written since
  2719.     the data contains relocation information which points to the
  2720.     symbols 
  2721.     */
  2722.   ieee_write_data_part(abfd);     
  2723.  
  2724.  
  2725.   /*
  2726.     At the end we put the end !
  2727.     */
  2728.   ieee_write_me_part(abfd);
  2729.  
  2730.  
  2731.   /* Generate the header */
  2732.   bfd_seek(abfd, old, false);
  2733.  
  2734.   for (i= 0; i < N_W_VARIABLES; i++) {
  2735.     ieee_write_2bytes(abfd,ieee_assign_value_to_variable_enum);
  2736.     ieee_write_byte(abfd, i);
  2737.     ieee_write_int5_out(abfd, ieee->w.offset[i]);
  2738.   }
  2739.   return true;
  2740. }
  2741.  
  2742.  
  2743.  
  2744.  
  2745. /* Native-level interface to symbols. */
  2746.  
  2747. /* We read the symbols into a buffer, which is discarded when this
  2748. function exits.  We read the strings into a buffer large enough to
  2749. hold them all plus all the cached symbol entries. */
  2750.  
  2751. asymbol *
  2752. DEFUN(ieee_make_empty_symbol,(abfd),
  2753.       bfd *abfd)
  2754. {
  2755.  
  2756.   ieee_symbol_type  *new =
  2757.     (ieee_symbol_type *)zalloc (sizeof (ieee_symbol_type));
  2758.   new->symbol.the_bfd = abfd;
  2759.   return &new->symbol;
  2760.  
  2761. }
  2762.  
  2763. static bfd *
  2764. DEFUN(ieee_openr_next_archived_file,(arch, prev),
  2765.       bfd *arch AND
  2766.       bfd *prev)
  2767. {
  2768.   ieee_ar_data_type *ar = ieee_ar_data(arch);
  2769.   /* take the next one from the arch state, or reset */
  2770.   if (prev == (bfd *)NULL) {
  2771.     /* Reset the index - the first two entries are bogus*/
  2772.     ar->element_index = 2;
  2773.   }
  2774.   while (true) {  
  2775.     ieee_ar_obstack_type *p = ar->elements + ar->element_index;
  2776.     ar->element_index++;
  2777.     if (ar->element_index <= ar->element_count) {
  2778.       if (p->file_offset != (file_ptr)0) {
  2779.     if (p->abfd == (bfd *)NULL) {
  2780.       p->abfd = _bfd_create_empty_archive_element_shell(arch);
  2781.       p->abfd->origin = p->file_offset;
  2782.     }
  2783.     return p->abfd;
  2784.       }
  2785.     }
  2786.     else {
  2787.       bfd_error = no_more_archived_files;
  2788.       return (bfd *)NULL;
  2789.     }
  2790.  
  2791.   }
  2792. }
  2793.  
  2794. static boolean
  2795. ieee_find_nearest_line(abfd,
  2796.              section,
  2797.              symbols,
  2798.              offset,
  2799.              filename_ptr,
  2800.              functionname_ptr,
  2801.              line_ptr)
  2802. bfd *abfd;
  2803. asection *section;
  2804. asymbol **symbols;
  2805. bfd_vma offset;
  2806. char **filename_ptr;
  2807. char **functionname_ptr;
  2808. int *line_ptr;
  2809. {
  2810.   return false;
  2811. }
  2812.  
  2813.  
  2814. static int
  2815. ieee_generic_stat_arch_elt(abfd, buf)
  2816. bfd *abfd;
  2817. struct stat *buf;
  2818. {
  2819.   ieee_ar_data_type *ar = ieee_ar_data(abfd);
  2820.   if (ar == (ieee_ar_data_type *)NULL) {
  2821.     bfd_error = invalid_operation;
  2822.     return -1;
  2823.   }
  2824.   else {
  2825.     buf->st_size = 0x1;
  2826.     buf->st_mode = 0666;
  2827.   return 0;
  2828.   }
  2829. }
  2830. static int 
  2831. DEFUN(ieee_sizeof_headers,(abfd, x),
  2832.       bfd *abfd AND
  2833.       boolean x)
  2834. {
  2835.   return 0;
  2836. }
  2837.  
  2838.  
  2839.  
  2840. static void 
  2841. DEFUN(ieee_bfd_debug_info_start,(abfd), 
  2842.       bfd *abfd)
  2843.   {
  2844.  
  2845.   }
  2846.  
  2847. static void 
  2848. DEFUN(ieee_bfd_debug_info_end,(abfd), 
  2849.       bfd *abfd)
  2850.   {
  2851.  
  2852.   }
  2853.  
  2854.  
  2855. /* Add this section to the list of sections we have debug info for, to
  2856.    be ready to output it at close time 
  2857.    */
  2858. static void 
  2859. DEFUN(ieee_bfd_debug_info_accumulate,(abfd, section), 
  2860.       bfd *abfd AND
  2861.       asection *section)
  2862. {
  2863.   ieee_data_type *ieee = ieee_data(section->owner);
  2864.   ieee_data_type *output_ieee = ieee_data(abfd);
  2865.   /* can only accumulate data from other ieee bfds */
  2866.   if (section->owner->xvec != abfd->xvec)
  2867.     return;
  2868.   /* Only bother once per bfd */
  2869.   if (ieee->done_debug == true) 
  2870.     return;
  2871.   ieee->done_debug = true;
  2872.  
  2873.   /* Don't bother if there is no debug info */
  2874.   if (ieee->w.r.debug_information_part == 0)
  2875.     return;
  2876.  
  2877.  
  2878.   /* Add to chain */
  2879.     {
  2880.       bfd_chain_type *n = (bfd_chain_type *) bfd_alloc(abfd, sizeof(bfd_chain_type));
  2881.       n->this = section->owner;
  2882.       n->next = (bfd_chain_type *)NULL;
  2883.     
  2884.       if (output_ieee->chain_head) {
  2885.     output_ieee->chain_head->next = n;
  2886.       }
  2887.       else {
  2888.     output_ieee->chain_root = n;
  2889.  
  2890.       }
  2891.     output_ieee->chain_head = n; 
  2892.     }
  2893. }
  2894.  
  2895.  
  2896.  
  2897.  
  2898.  
  2899.  
  2900. #define FOO PROTO
  2901. #define ieee_core_file_failing_command (char *(*)())(bfd_nullvoidptr)
  2902. #define ieee_core_file_failing_signal (int (*)())bfd_0
  2903. #define ieee_core_file_matches_executable_p ( FOO(boolean, (*),(bfd *, bfd *)))bfd_false
  2904. #define ieee_slurp_armap bfd_true
  2905. #define ieee_slurp_extended_name_table bfd_true
  2906. #define ieee_truncate_arname (void (*)())bfd_nullvoidptr
  2907. #define ieee_write_armap  (FOO( boolean, (*),(bfd *, unsigned int, struct orl *, int, int))) bfd_nullvoidptr
  2908. #define ieee_get_lineno (struct lineno_cache_entry *(*)())bfd_nullvoidptr
  2909. #define    ieee_close_and_cleanup        bfd_generic_close_and_cleanup
  2910. #define ieee_set_arch_mach bfd_default_set_arch_mach
  2911.  
  2912. /*SUPPRESS 460 */
  2913. bfd_target ieee_vec =
  2914. {
  2915.   "ieee",            /* name */
  2916.   bfd_target_ieee_flavour,
  2917.   true,                /* target byte order */
  2918.   true,                /* target headers byte order */
  2919.   (HAS_RELOC | EXEC_P |        /* object flags */
  2920.    HAS_LINENO | HAS_DEBUG |
  2921.    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
  2922.   (SEC_CODE|SEC_DATA|SEC_ROM|SEC_HAS_CONTENTS
  2923.    |SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
  2924.   ' ',                /* ar_pad_char */
  2925.   16,                /* ar_max_namelen */
  2926.     1,                /* minimum alignment */
  2927. _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* data */
  2928. _do_getb64, _do_putb64,  _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */
  2929.  
  2930.   { _bfd_dummy_target,
  2931.      ieee_object_p,        /* bfd_check_format */
  2932.      ieee_archive_p,
  2933.     _bfd_dummy_target,
  2934.      },
  2935.   {
  2936.     bfd_false,
  2937.     ieee_mkobject, 
  2938.     _bfd_generic_mkarchive,
  2939.     bfd_false
  2940.     },
  2941.   {
  2942.     bfd_false,
  2943.     ieee_write_object_contents,
  2944.     _bfd_write_archive_contents,
  2945.     bfd_false,
  2946.   },
  2947.   JUMP_TABLE(ieee)
  2948. };
  2949.